TShopping

 找回密碼
 註冊
搜索
查看: 624|回復: 0

[教學] [Android智慧型手機設計_Ch9_4_MediaPlay_SurfaceView播放器]

[複製鏈接]
發表於 2013-7-1 21:05:35 | 顯示全部樓層 |閱讀模式
 
Push to Facebook Push to Plurk Push to Twitter 
1.jpg


MediaPlayer可以播放video與audio檔案

播放video檔案必須先準備一個播放畫面

SurfaceView支援高速繪圖,適合用來呈現video的播放、拍照前的預覽

雖然SurfaceView是主要呈現畫面的地方,但是控制內部surface運作的卻是SurfaceHolder

SurfaceHolder.Callback介面定義了與surface生命週期有關的3個重要
方法:
surfaceCreated() surface第一次建立時呼叫
surfaceChanged() surface改變時呼叫
surfaceDestroyed() surface被破壞時呼叫


1.設計Ch9_4_MediaPlay_SurfaceView.java

  1. package com.example.ch9_4_mediaplay_surfaceview;

  2. import java.io.IOException;

  3. import android.media.AudioManager;
  4. import android.media.MediaPlayer;
  5. import android.media.MediaPlayer.OnBufferingUpdateListener;
  6. import android.media.MediaPlayer.OnCompletionListener;
  7. import android.media.MediaPlayer.OnPreparedListener;
  8. import android.media.MediaPlayer.OnVideoSizeChangedListener;
  9. import android.os.Bundle;
  10. import android.app.Activity;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.SurfaceHolder;
  14. import android.view.SurfaceView;

  15. public class Ch9_4_MediaPlay_SurfaceView extends Activity {
  16. SurfaceView sv;
  17. SurfaceHolder sh;
  18. int videowidth;
  19. int videoheight;
  20. boolean videoReady=false;
  21. boolean videoSize=false;
  22. MediaPlayer mp;

  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.main);
  27. sv=(SurfaceView)findViewById(R.id.surfaceView1);
  28. sh=sv.getHolder();
  29. sh.addCallback(new surfaceCallback());
  30. }
  31. class surfaceCallback implements SurfaceHolder.Callback {

  32. @Override
  33. public void surfaceChanged(SurfaceHolder holder, int format, int width,
  34. int height) {
  35. videowidth=0;
  36. videoheight=0;
  37. videoReady=false;
  38. videoSize=false;
  39. if(mp ==null)
  40. mp=new MediaPlayer();
  41. try {
  42. mp.setDataSource("/mnt/sdcard/baby.mp4");
  43. mp.setDisplay(sh);
  44. mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
  45. mp.prepare();
  46. //mp.start();
  47. mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener(){
  48. @Override
  49. public void onBufferingUpdate(MediaPlayer mp,
  50. int percent) {
  51. Log.d("ch9_4","緩衝區更新比例"+percent);
  52. }});
  53. mp.setOnCompletionListener(new OnCompletionListener(){

  54. @Override
  55. public void onCompletion(MediaPlayer mp) {
  56. Log.d("ch9_4","播放完畢!");
  57. }});
  58. mp.setOnPreparedListener(new OnPreparedListener(){
  59. @Override
  60. public void onPrepared(MediaPlayer mp) {
  61. videoReady=true;
  62. if(videoSize){
  63. sh.setFixedSize(videowidth, videoheight);
  64. mp.start();
  65. }
  66. }});
  67. mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener(){

  68. @Override
  69. public void onVideoSizeChanged(MediaPlayer mp,
  70. int width, int height) {
  71. if(width==0 ||height==0)
  72. Log.e("C9_4", "Video的寬度或高度有異常");
  73. videoSize=true;
  74. videowidth=width;
  75. videoheight=height;
  76. if(videoReady){
  77. sh.setFixedSize(videowidth, videoheight);
  78. mp.start();
  79. }
  80. }});

  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. }

  84. }

  85. @Override
  86. public void surfaceCreated(SurfaceHolder holder) {
  87. Log.d("Ch9_4","surfaceCreated被呼叫!");
  88. }

  89. @Override
  90. public void surfaceDestroyed(SurfaceHolder holder) {
  91. if(mp !=null){
  92. mp.release();
  93. mp=null;
  94. }
  95. videowidth=0;
  96. videoheight=0;
  97. videoReady=false;
  98. videoSize=false;
  99. }
  100. }
  101. }
複製代碼


 

臉書網友討論
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2016-12-7 12:05 , Processed in 0.055778 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表