Android应用开发之简易视频播放器


在Android中播放视频用到的也是MediaPlayer,展示视频通常使用SurfaceView控件。

在main.xml布局文件添加用于视频画面绘制的SurfaceView 控件:

<SurfaceView android:layout_width="fill_parent"android:layout_height="240dip"android:id="@+id/surfaceView"/>

MeidaPlayer播放视频相关API使用方法:

  1. SurfaceView surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);  
  2. surfaceView.getHolder().setFixedSize(176144);  //设置分辨率   
  3. /*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/  
  4. surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
  5. MediaPlayer mediaPlayer = new MediaPlayer();  
  6. mediaPlayer.reset();//重置为初始状态   
  7. mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
  8. /* 设置Video影片以SurfaceHolder播放 */  
  9. mediaPlayer.setDisplay(surfaceView.getHolder());  
  10. mediaPlayer.setDataSource("/mnt/sdcard/oppo.mp4");  
  11. mediaPlayer.prepare();//缓冲    
  12. mediaPlayer.start();//播放   
  13. mediaPlayer.pause();//暂停播放   
  14. mediaPlayer.start();//恢复播放   
  15. mediaPlayer.stop();//停止播放   
  16. mediaPlayer.release();//释放资源  
下面介绍一个播放视频的简易例子,在模拟器上运行时最好用2.1, 2.1以后的模拟器播放视频时会有问题,视频播放同样需要处理一些来电类的情况,需要覆写Activity生命周期里的几个方法,需要注意的是,当Surface控件被其他Activity遮挡住是,会被销毁,所以需要为他写一个回调函数,否则当当前Activity被其他Activity打断后,又回到当前Activity是,视频播放界面会是黑屏。

Android应用开发之简易视频播放器

详细代码:

布局文件layout/main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:background="#ffffff"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8. <TextView    
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="@string/filename"  
  12.     />  
  13.       
  14.     <EditText  
  15.         android:layout_width="fill_parent"   
  16.         android:layout_height="wrap_content"   
  17.         android:text="oppo.mp4"  
  18.         android:id="@+id/filename"  
  19.         />  
  20.           
  21.     <LinearLayout  
  22.         android:orientation="horizontal"  
  23.         android:layout_width="fill_parent"  
  24.         android:layout_height="wrap_content"  
  25.         >  
  26.         <ImageButton  
  27.             android:layout_width="wrap_content"   
  28.             android:layout_height="wrap_content"   
  29.             android:src="@drawable/play"  
  30.             android:id="@+id/play"  
  31.             />  
  32.          <ImageButton  
  33.             android:layout_width="wrap_content"   
  34.             android:layout_height="wrap_content"   
  35.             android:src="@drawable/pause"  
  36.             android:id="@+id/pause"  
  37.             />  
  38.         <ImageButton  
  39.             android:layout_width="wrap_content"   
  40.             android:layout_height="wrap_content"   
  41.             android:src="@drawable/reset"  
  42.             android:id="@+id/reset"  
  43.             />  
  44.          <ImageButton  
  45.             android:layout_width="wrap_content"   
  46.             android:layout_height="wrap_content"   
  47.             android:src="@drawable/stop"  
  48.             android:id="@+id/stop"  
  49.             />  
  50.     </LinearLayout>  
  51.       
  52.     <SurfaceView  
  53.         android:layout_width="fill_parent"   
  54.         android:layout_height="240dip"   
  55.         android:id="@+id/surfaceView"  
  56.         />  
  57. </LinearLayout>  
  • 1
  • 2
  • 下一页

相关内容