Android笔记:视屏播放、VideoView、surfaceView,简易视频播放
一、VideoView方法
1.activity_video.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".VideoActivity"><VideoViewandroid:id="@+id/video_videoView"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"/></RelativeLayout>
2.代码
packagecom.example.vediotest;importandroid.media.MediaPlayer;importandroid.net.Uri;importandroid.os.Bundle;importandroid.app.Activity;importandroid.content.pm.ActivityInfo;importandroid.view.Menu;importandroid.view.Window;importandroid.view.WindowManager;importandroid.widget.MediaController;importandroid.widget.VideoView;publicclassVideoActivityextendsActivity{privateVideoViewvideoView;privateUrimUri;privateintmPositionWhenPaused;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置成全屏模式setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏setContentView(R.layout.activity_video);Stringurl="http://videofile.xxxx.cn/Upload/Video/File/20140411/201404110228168972.mp4";//Stringurl=//"http://player.youku.com/player.php/sid/XNDYwOTEzNzQ4/v.swf";mUri=Uri.parse(url);videoView=(VideoView)findViewById(R.id.video_videoView);MediaControllermediaController=newMediaController(this);videoView.setMediaController(mediaController);//videoView.setVideoPath("/sdcard/xyx.3gp");//videoView.setVideoURI(mUri);//videoView.requestFocus();//videoView.start();}publicvoidonStart(){//PlayVideovideoView.setVideoURI(mUri);videoView.start();super.onStart();}publicvoidonPause(){//Stopvideowhentheactivityispause.mPositionWhenPaused=videoView.getCurrentPosition();videoView.stopPlayback();super.onPause();}publicvoidonResume(){//Resumevideoplayerif(mPositionWhenPaused>=0){videoView.seekTo(mPositionWhenPaused);mPositionWhenPaused=-1;}super.onResume();}publicbooleanonError(MediaPlayerplayer,intarg1,intarg2){returnfalse;}publicvoidonCompletion(MediaPlayermp){this.finish();}}
二、surfaceView方法
(一)
1.activity_video_surface.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="标题"/><SurfaceViewandroid:id="@+id/surfaceVideo_surfaceView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"></SurfaceView></LinearLayout>
2.代码
packagecom.example.vediotest;importandroid.media.AudioManager;importandroid.media.MediaPlayer;importandroid.media.MediaPlayer.OnBufferingUpdateListener;importandroid.media.MediaPlayer.OnCompletionListener;importandroid.media.MediaPlayer.OnPreparedListener;importandroid.net.Uri;importandroid.os.Bundle;importandroid.app.Activity;importandroid.util.Log;importandroid.view.Menu;importandroid.view.SurfaceHolder;importandroid.view.SurfaceHolder.Callback;importandroid.view.SurfaceView;publicclassSurfaceVideoActivityextendsActivityimplementsCallback,OnBufferingUpdateListener,OnCompletionListener,OnPreparedListener{privateintwidth=0;privateintheight=0;privateMediaPlayermMediaPlayer=null;privateSurfaceViewmSurfaceView=null;privateSurfaceHolderholder=null;privateStringpath="";@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_video_surface);mSurfaceView=(SurfaceView)this.findViewById(R.id.surfaceVideo_surfaceView);holder=mSurfaceView.getHolder();holder.addCallback(this);holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//设置风格}publicvoidplayVedio(){try{path=android.os.Environment.getExternalStorageDirectory()+"/moto_0012.3gp";mMediaPlayer=newMediaPlayer();//mMediaPlayer.setDataSource(path);Stringurl="http://videofile.housebox.cn/Upload/Video/File/20140411/201404110228168972.mp4";//Stringurl="http://player.youku.com/player.php/sid/XNDYwOTEzNzQ4/v.swf";mMediaPlayer.setDataSource(this,Uri.parse(url));mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);mMediaPlayer.setDisplay(holder);mMediaPlayer.prepare();//准备Log.e("TAG-Duration",mMediaPlayer.getDuration()+"");mMediaPlayer.setOnBufferingUpdateListener(this);mMediaPlayer.setOnCompletionListener(this);mMediaPlayer.setOnPreparedListener(this);}catch(Exceptionex){}}publicvoidonBufferingUpdate(MediaPlayermp,intpercent){//TODOAuto-generatedmethodstubLog.i("TAG-onBufferingUpdate",percent+"|"+mMediaPlayer.getCurrentPosition());}publicvoidonCompletion(MediaPlayermp){//TODOAuto-generatedmethodstubLog.i("TAG-onCompletion","Completion");}publicvoidonPrepared(MediaPlayermp){//TODOAuto-generatedmethodstubwidth=mMediaPlayer.getVideoWidth();height=mMediaPlayer.getVideoHeight();if(width!=0&&height!=0){holder.setFixedSize(width,height);//设置视频高宽mMediaPlayer.start();Log.i("TAG-Duration2",mMediaPlayer.getDuration()+"");}}publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,intheight){//TODOAuto-generatedmethodstub}publicvoidsurfaceCreated(SurfaceHolderholder){//TODOAuto-generatedmethodstubplayVedio();}publicvoidsurfaceDestroyed(SurfaceHolderholder){//TODOAuto-generatedmethodstubLog.i("TAG-surfaceDestroyed","surfaceDestroyed");}@OverrideprotectedvoidonPause(){super.onPause();if(mMediaPlayer!=null){if(mMediaPlayer.isPlaying()){mMediaPlayer.stop();}mMediaPlayer.reset();mMediaPlayer.release();mMediaPlayer=null;}}}
(二)
activity_video_surface2.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><SurfaceViewandroid:id="@+id/surface2_surfaceView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"></SurfaceView></RelativeLayout>
2.代码
packagecom.example.vediotest;importjava.io.IOException;importandroid.media.AudioManager;importandroid.media.MediaPlayer;importandroid.media.MediaPlayer.OnBufferingUpdateListener;importandroid.media.MediaPlayer.OnCompletionListener;importandroid.net.Uri;importandroid.os.Bundle;importandroid.app.Activity;importandroid.content.pm.ActivityInfo;importandroid.util.Log;importandroid.view.Menu;importandroid.view.SurfaceHolder;importandroid.view.SurfaceView;importandroid.view.Window;publicclassSurfaceVideo2ActivityextendsActivityimplementsOnBufferingUpdateListener,OnCompletionListener,MediaPlayer.OnPreparedListener,SurfaceHolder.Callback{privateMediaPlayermediaPlayer;privateSurfaceViewsurfaceView;privateSurfaceHoldersurfaceHolder;privateintvideoWidth;privateintvideoHeight;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_video_surface2);setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏this.surfaceView=(SurfaceView)this.findViewById(R.id.surface2_surfaceView);this.surfaceHolder=this.surfaceView.getHolder();this.surfaceHolder.addCallback(this);this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);Log.v("cat",">>>createok.");}privatevoidplayVideo()throwsIllegalArgumentException,IllegalStateException,IOException{Stringurl="http://videofile.housebox.cn/Upload/Video/File/20140411/201404110228168972.mp4";//Stringurl="http://player.youku.com/player.php/sid/XNDYwOTEzNzQ4/v.swf";this.mediaPlayer=newMediaPlayer();//this.mediaPlayer.setDataSource("/sdcard/daoxiang.3gp");this.mediaPlayer.setDataSource(this,Uri.parse(url));this.mediaPlayer.setDisplay(this.surfaceHolder);this.mediaPlayer.prepare();this.mediaPlayer.setOnBufferingUpdateListener(this);this.mediaPlayer.setOnPreparedListener(this);this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);Log.i("mplayer",">>>playvideo");}@OverridepublicvoidsurfaceChanged(SurfaceHolderarg0,intarg1,intarg2,intarg3){Log.i("cat",">>>surfacechanged");}@OverridepublicvoidsurfaceCreated(SurfaceHolderholder){try{this.playVideo();}catch(Exceptione){Log.i("cat",">>>error",e);}Log.i("cat",">>>surfacecreated");}@OverridepublicvoidsurfaceDestroyed(SurfaceHolderholder){Log.v("mplayer",">>>surfacedestroyed");}@OverridepublicvoidonCompletion(MediaPlayerarg0){//TODOAuto-generatedmethodstub}@OverridepublicvoidonBufferingUpdate(MediaPlayermp,intpercent){//TODOAuto-generatedmethodstub}@OverridepublicvoidonPrepared(MediaPlayerarg0){this.videoWidth=this.mediaPlayer.getVideoWidth();this.videoHeight=this.mediaPlayer.getVideoHeight();if(this.videoHeight!=0&&this.videoWidth!=0){this.surfaceHolder.setFixedSize(this.videoWidth,this.videoHeight);this.mediaPlayer.start();}}@OverrideprotectedvoidonDestroy(){super.onDestroy();if(this.mediaPlayer!=null){this.mediaPlayer.release();this.mediaPlayer=null;}}}
参考资料:
1.http://blog.csdn.net/cynhafa/article/details/6400050
2.http://aina-hk55hk.iteye.com/blog/706060
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。