Android三种播放视频的方式

在Android中,我们有三种方式来实现视频的播放:

1、使用其自带的播放器。指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型。

2、使用VideoView来播放。在布局文件中使用VideoView结合MediaController来实现对其控制。

3、使用MediaPlayer类和SurfaceView来实现,这种方式很灵活。


1、调用其自带的播放器:

[java] view plain copy print ?
  1. Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v");
  2. //调用系统自带的播放器
  3. Intent intent = new Intent(Intent.ACTION_VIEW);
  4. Log.v("URI:::::::::", uri.toString());
  5. intent.setDataAndType(uri, "video/mp4");
  6. startActivity(intent);

2、使用VideoView来实现:

[java] view plain copy print ?
  1. Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v");
  2. VideoView videoView = (VideoView)this.findViewById(R.id.video_view);
  3. videoView.setMediaController(new MediaController(this));
  4. videoView.setVideoURI(uri);
  5. videoView.start();
  6. videoView.requestFocus();

3、使用MediaPlayer:

[java] view plain copy print ?
  1. package demo.camera;
  2. import java.io.IOException;
  3. import android.app.Activity;
  4. import android.media.MediaPlayer;
  5. import android.media.MediaPlayer.OnCompletionListener;
  6. import android.media.MediaPlayer.OnErrorListener;
  7. import android.media.MediaPlayer.OnInfoListener;
  8. import android.media.MediaPlayer.OnPreparedListener;
  9. import android.media.MediaPlayer.OnSeekCompleteListener;
  10. import android.media.MediaPlayer.OnVideoSizeChangedListener;
  11. import android.os.Bundle;
  12. import android.os.Environment;
  13. import android.util.Log;
  14. import android.view.Display;
  15. import android.view.SurfaceHolder;
  16. import android.view.SurfaceView;
  17. import android.widget.LinearLayout;
  18. /**
  19. * 该实例中使用MediaPlayer完成播放,同时界面使用SurfaceView来实现
  20. *
  21. * 这里我们实现MediaPlayer中很多状态变化时的监听器
  22. *
  23. * 使用Mediaplayer时,也可以使用MediaController类,但是需要实现MediaController.mediaController接口
  24. * 实现一些控制方法。
  25. *
  26. * 然后,设置controller.setMediaPlayer(),setAnchorView(),setEnabled(),show()就可以了,这里不再实现
  27. * @author Administrator
  28. *
  29. */
  30. public class VideoSurfaceDemo extends Activity implements OnCompletionListener,OnErrorListener,OnInfoListener,
  31. OnPreparedListener, OnSeekCompleteListener,OnVideoSizeChangedListener,SurfaceHolder.Callback{
  32. private Display currDisplay;
  33. private SurfaceView surfaceView;
  34. private SurfaceHolder holder;
  35. private MediaPlayer player;
  36. private int vWidth,vHeight;
  37. //private boolean readyToPlay = false;
  38. public void onCreate(Bundle savedInstanceState){
  39. super.onCreate(savedInstanceState);
  40. this.setContentView(R.layout.video_surface);
  41. surfaceView = (SurfaceView)this.findViewById(R.id.video_surface);
  42. //给SurfaceView添加CallBack监听
  43. holder = surfaceView.getHolder();
  44. holder.addCallback(this);
  45. //为了可以播放视频或者使用Camera预览,我们需要指定其Buffer类型
  46. holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  47. //下面开始实例化MediaPlayer对象
  48. player = new MediaPlayer();
  49. player.setOnCompletionListener(this);
  50. player.setOnErrorListener(this);
  51. player.setOnInfoListener(this);
  52. player.setOnPreparedListener(this);
  53. player.setOnSeekCompleteListener(this);
  54. player.setOnVideoSizeChangedListener(this);
  55. Log.v("Begin:::", "surfaceDestroyed called");
  56. //然后指定需要播放文件的路径,初始化MediaPlayer
  57. String dataPath = Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v";
  58. try {
  59. player.setDataSource(dataPath);
  60. Log.v("Next:::", "surfaceDestroyed called");
  61. } catch (IllegalArgumentException e) {
  62. e.printStackTrace();
  63. } catch (IllegalStateException e) {
  64. e.printStackTrace();
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. //然后,我们取得当前Display对象
  69. currDisplay = this.getWindowManager().getDefaultDisplay();
  70. }
  71. @Override
  72. public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  73. // 当Surface尺寸等参数改变时触发
  74. Log.v("Surface Change:::", "surfaceChanged called");
  75. }
  76. @Override
  77. public void surfaceCreated(SurfaceHolder holder) {
  78. // 当SurfaceView中的Surface被创建的时候被调用
  79. //在这里我们指定MediaPlayer在当前的Surface中进行播放
  80. player.setDisplay(holder);
  81. //在指定了MediaPlayer播放的容器后,我们就可以使用prepare或者prepareAsync来准备播放了
  82. player.prepareAsync();
  83. }
  84. @Override
  85. public void surfaceDestroyed(SurfaceHolder holder) {
  86. Log.v("Surface Destory:::", "surfaceDestroyed called");
  87. }
  88. @Override
  89. public void onVideoSizeChanged(MediaPlayer arg0, int arg1, int arg2) {
  90. // 当video大小改变时触发
  91. //这个方法在设置player的source后至少触发一次
  92. Log.v("Video Size Change", "onVideoSizeChanged called");
  93. }
  94. @Override
  95. public void onSeekComplete(MediaPlayer arg0) {
  96. // seek操作完成时触发
  97. Log.v("Seek Completion", "onSeekComplete called");
  98. }
  99. @Override
  100. public void onPrepared(MediaPlayer player) {
  101. // 当prepare完成后,该方法触发,在这里我们播放视频
  102. //首先取得video的宽和高
  103. vWidth = player.getVideoWidth();
  104. vHeight = player.getVideoHeight();
  105. if(vWidth > currDisplay.getWidth() || vHeight > currDisplay.getHeight()){
  106. //如果video的宽或者高超出了当前屏幕的大小,则要进行缩放
  107. float wRatio = (float)vWidth/(float)currDisplay.getWidth();
  108. float hRatio = (float)vHeight/(float)currDisplay.getHeight();
  109. //选择大的一个进行缩放
  110. float ratio = Math.max(wRatio, hRatio);
  111. vWidth = (int)Math.ceil((float)vWidth/ratio);
  112. vHeight = (int)Math.ceil((float)vHeight/ratio);
  113. //设置surfaceView的布局参数
  114. surfaceView.setLayoutParams(new LinearLayout.LayoutParams(vWidth, vHeight));
  115. //然后开始播放视频
  116. player.start();
  117. }
  118. }
  119. @Override
  120. public boolean onInfo(MediaPlayer player, int whatInfo, int extra) {
  121. // 当一些特定信息出现或者警告时触发
  122. switch(whatInfo){
  123. case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:
  124. break;
  125. case MediaPlayer.MEDIA_INFO_METADATA_UPDATE:
  126. break;
  127. case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
  128. break;
  129. case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE:
  130. break;
  131. }
  132. return false;
  133. }
  134. @Override
  135. public boolean onError(MediaPlayer player, int whatError, int extra) {
  136. Log.v("Play Error:::", "onError called");
  137. switch (whatError) {
  138. case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
  139. Log.v("Play Error:::", "MEDIA_ERROR_SERVER_DIED");
  140. break;
  141. case MediaPlayer.MEDIA_ERROR_UNKNOWN:
  142. Log.v("Play Error:::", "MEDIA_ERROR_UNKNOWN");
  143. break;
  144. default:
  145. break;
  146. }
  147. return false;
  148. }
  149. @Override
  150. public void onCompletion(MediaPlayer player) {
  151. // 当MediaPlayer播放完成后触发
  152. Log.v("Play Over:::", "onComletion called");
  153. this.finish();
  154. }
  155. }    
 
 

你可能感兴趣的:(Android三种播放视频的方式)