Android VideoView的使用例程

一、 xxxvideo.java

public class RemoteVideo extends Activity {

private String path = "";

private String textUrl = "";

private VideoView mVideoView;

private static int i = 0;

private int width;

private int heigh;

private Dialog dialog;

private Handler mHandler = new Handler();


public void onCreate(Bundle icicle) {

super.onCreate(icicle);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Bundle bundle = this.getIntent().getExtras();

DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

width=dm.widthPixels;

heigh=dm.heightPixels;

textUrl = bundle.getString("url");

if(width/heigh>0)

{

setContentView(R.layout.videoview);

path = bundle.getString("widthurl");

Log.i("mp4", "heng"+path);

}

if(width/heigh==0)

{

setContentView(R.layout.view);

path = bundle.getString("heighturl");

Log.i("mp4", "shu"+path);

}

dialog=ProgressDialog.show(this, "视频加载中...", "请您稍候");

mVideoView = (VideoView) findViewById(R.id.surface_view);

mVideoView.setVideoPath(path);

MediaController controller = new MediaController(this);

mVideoView.setMediaController(controller);

mVideoView.requestFocus();

mVideoView.setOnPreparedListener(new OnPreparedListener() {

//@Override

public void onPrepared(MediaPlayer mp) {

mVideoView.setBackgroundColor(Color.argb(0, 0, 255, 0));

dialog.dismiss();

}

});

mVideoView.setOnCompletionListener(new OnCompletionListener() {

//@Override

public void onCompletion(MediaPlayer mp) {

Toast.makeText(RemoteVideo.this, "video play finished!", Toast.LENGTH_LONG)

.show();

}

});

controller.setOnTouchListener(new View.OnTouchListener() {


public boolean onTouch(View v, MotionEvent event) {

return true;

}

});

}


@Override

protected void onResume() {

super.onResume();

mVideoView.seekTo(i);

mVideoView.start();

}

@Override

protected void onStop() {

super.onStop();

mVideoView.pause();

i = mVideoView.getCurrentPosition();

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

finish();

return true;

}

return false;

}

}


说明:

1. VideoView可接收本地和远程的视频源并解码;

2. MediaController为videoview的控制条类;

3. bundle获取intent传过来的视频路径;

二、videoview.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent" android:layout_height="fill_parent"

android:gravity="left" android:orientation="horizontal">

<VideoView android:id="@+id/surface_view"

android:layout_width="176px" android:layout_height="132px"

android:layout_gravity="left" />

<WebView android:id="@+id/web_openonline"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

</LinearLayout>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwswxx.html