Urchindong 2011-04-29
简介
用来播放视频文件。该VideoView类可以加载各种来源的图像(如资源或内容提供商),需要计算它从视频测量,以便它可以在任何布局管理器使用,并提供诸如缩放和着色的各种显示选项。在其他的平台上面可能VideoPlayer开发是一个比较有挑战性的工作,但是在Android上面VideoPlayer的开发,基本上可以做到傻瓜式啦。本文简单对VideoPlayer的开发进行简单的介绍。
实例
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<VideoViewandroid:id="@+id/videoview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</LinearLayout>
2.Java代码
package com.wjq;
import android.app.Activity;
importandroid.content.pm.ActivityInfo;
importandroid.media.MediaPlayer;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.util.Log;
importandroid.widget.MediaController;
importandroid.widget.VideoView;
publicclassVideoViewDemoextendsActivityimplementsMediaPlayer.OnErrorListener,MediaPlayer.OnCompletionListener{
publicstaticfinalStringTAG="VideoPlayer";
privateVideoViewmVideoView;
privateUrimUri;
private int mPositionWhenPaused = -1;private MediaController mMediaController;
/* (non-Javadoc)
*@seeandroid.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protectedvoidonCreate(BundlesavedInstanceState){
//TODOAuto-generatedmethodstub
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mVideoView=(VideoView)findViewById(R.id.videoview);
//文件路径
mUri = Uri.parse(Environment.getExternalStorageDirectory() + "/ziranyouli.3gp");
//Create media controller mMediaController = new MediaController(this);
//设置MediaController mVideoView.setMediaController(mMediaController);
}
//监听MediaPlayer上报的错误信息
@Override
publicbooleanonError(MediaPlayermp,intwhat,intextra){
//TODOAuto-generatedmethodstub
returnfalse;
}//Video播完的时候得到通知
@Override
publicvoidonCompletion(MediaPlayermp){
this.finish();
}
//开始
publicvoidonStart(){
//PlayVideo
mVideoView.setVideoURI(mUri);
mVideoView.start();super.onStart(); }
//暂停
public void onPause() {
//Stopvideowhentheactivityispause.
mPositionWhenPaused=mVideoView.getCurrentPosition();
mVideoView.stopPlayback();
Log.d(TAG,"OnStop:mPositionWhenPaused="+mPositionWhenPaused);
Log.d(TAG, "OnStop: getDuration = " + mVideoView.getDuration());super.onPause(); }
public void onResume() {
//Resumevideoplayer
if(mPositionWhenPaused>=0){
mVideoView.seekTo(mPositionWhenPaused);
mPositionWhenPaused=-1;
}super.onResume(); }
}