歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發之簡易視頻播放器

Android應用開發之簡易視頻播放器

日期:2017/3/1 10:27:57   编辑:Linux編程

在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(176, 144); //設置分辨率
  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是,視頻播放界面會是黑屏。

詳細代碼:

布局文件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. <EditText
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text="oppo.mp4"
  17. android:id="@+id/filename"
  18. />
  19. <LinearLayout
  20. android:orientation="horizontal"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. >
  24. <ImageButton
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:src="@drawable/play"
  28. android:id="@+id/play"
  29. />
  30. <ImageButton
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:src="@drawable/pause"
  34. android:id="@+id/pause"
  35. />
  36. <ImageButton
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:src="@drawable/reset"
  40. android:id="@+id/reset"
  41. />
  42. <ImageButton
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:src="@drawable/stop"
  46. android:id="@+id/stop"
  47. />
  48. </LinearLayout>
  49. <SurfaceView
  50. android:layout_width="fill_parent"
  51. android:layout_height="240dip"
  52. android:id="@+id/surfaceView"
  53. />
  54. </LinearLayout>
Copyright © Linux教程網 All Rights Reserved