Android利用VideoView实现VideoPlayer
2010-10-14 06:24:00 来源:本站整理/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set the screen to landscape.
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mVideoView = (VideoView)findViewById(R.id.video_view);
//Video file
mUri = Uri.parse(Environment.getExternalStorageDirectory() + "/1.3gp");
//Create media controller
mMediaController = new MediaController(this);
mVideoView.setMediaController(mMediaController);
}
public void onStart() {
// Play Video
mVideoView.setVideoURI(mUri);
mVideoView.start();
super.onStart();
}
public void onPause() {
// Stop video when the activity is pause.
mPositionWhenPaused = mVideoView.getCurrentPosition();
mVideoView.stopPlayback();
Log.d(TAG, "OnStop: mPositionWhenPaused = " + mPositionWhenPaused);
Log.d(TAG, "OnStop: getDuration = " + mVideoView.getDuration());
super.onPause();
}
public void onResume() {
// Resume video player
if(mPositionWhenPaused >= 0) {
mVideoView.seekTo(mPositionWhenPaused);
mPositionWhenPaused = -1;
}
super.onResume();
}
public boolean onError(MediaPlayer player, int arg1, int arg2) {
return false;
}
public void onCompletion(MediaPlayer mp) {
this.finish();
}
}
本例中只是播放外存储器(一般是sd卡)上的1.3gp文件。在onCreate方法中创建了Media control,这个组件可以控制视频的播放,暂停,回复,seek等操作,不需要你实现。
//Create media controller
mMediaController = new MediaController(this);
更多精彩
赞助商链接