Android多媒体学习⼗⼆:Android中Video的三种播放⽅式的实现Android多媒体学习⼗⼆:Android中Video的三种播放⽅式的实现
分类:Android 学习笔记 2011-04-21 11:29 1432⼈阅读评论(2) 收藏举报
在Android中,我们有三种⽅式来实现视频的播放。
1、使⽤其⾃带的播放器。指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型。
2、使⽤VideoView来播放。在布局⽂件中使⽤VideoView结合MediaController来实现对其控制。
3、使⽤MediaPlayer类和SurfaceView来实现。这种⽅式最灵活,也最复杂。
下⾯给出这三种⽅式,
1、调⽤其⾃带的播放器:
[java]view plaincopy?
1. Uri uri = Uri.ExternalStorageDirectory().getPath()+"/Test_Movie.m4v");
2. //调⽤系统⾃带的播放器
3. Intent intent = new Intent(Intent.ACTION_VIEW);
4. Log.v("URI:::::::::", String());
5. intent.setDataAndType(uri, "video/mp4");
6. startActivity(intent);
2、使⽤VideoView来实现:
[java]view plaincopy?
1. Uri uri = Uri.ExternalStorageDirectory().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. questFocus();
3、使⽤MediaPlayer:
[java]view plaincopy?
1. package demo.camera;
2. import java.io.IOException;
3. import android.app.Activity;
4. dia.MediaPlayer;
5. dia.MediaPlayer.OnCompletionListener;
6. dia.MediaPlayer.OnErrorListener;
7. dia.MediaPlayer.OnInfoListener;
8. dia.MediaPlayer.OnPreparedListener;
9. dia.MediaPlayer.OnSeekCompleteListener;
10. dia.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类,但是需要实现diaController接⼝
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.
39.
40. public void onCreate(Bundle savedInstanceState){
41. Create(savedInstanceState);
42. this.setContentView(R.layout.video_surface);
43.
44.
45. surfaceView = (SurfaceView)this.findViewById(R.id.video_surface);
46. //给SurfaceView添加CallBack监听
47. holder = Holder();
48. holder.addCallback(this);
49. //为了可以播放视频或者使⽤Camera预览,我们需要指定其Buffer类型
50. holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
51.
52. //下⾯开始实例化MediaPlayer对象
53. player = new MediaPlayer();
54. player.setOnCompletionListener(this);
55. player.setOnErrorListener(this);
56. player.setOnInfoListener(this);
57. player.setOnPreparedListener(this);
58. player.setOnSeekCompleteListener(this);
59. player.setOnVideoSizeChangedListener(this);
60. Log.v("Begin:::", "surfaceDestroyed called");
61. //然后指定需要播放⽂件的路径,初始化MediaPlayer
62. String dataPath = ExternalStorageDirectory().getPath()+"/Test_Movie.m4v";
63. try {
64. player.setDataSource(dataPath);
65. Log.v("Next:::", "surfaceDestroyed called");
66. } catch (IllegalArgumentException e) {
67. // TODO Auto-generated catch block
68. e.printStackTrace();
69. } catch (IllegalStateException e) {
70. // TODO Auto-generated catch block
71. e.printStackTrace();
72. } catch (IOException e) {
73. // TODO Auto-generated catch block
74. e.printStackTrace();
75. }
76.
77. //然后,我们取得当前Display对象
78. currDisplay = WindowManager().getDefaultDisplay();
79. }
80.
81. @Override
82. public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
83. // 当Surface尺⼨等参数改变时触发
84. Log.v("Surface Change:::", "surfaceChanged called");
85. }
86. @Override
87. public void surfaceCreated(SurfaceHolder holder) {
88. // 当SurfaceView中的Surface被创建的时候被调⽤
89. //在这⾥我们指定MediaPlayer在当前的Surface中进⾏播放
90. player.setDisplay(holder);
android学习教程91. //在指定了MediaPlayer播放的容器后,我们就可以使⽤prepare或者prepareAsync来准备播放了
92. player.prepareAsync();
93.
94. }
95. @Override
96. public void surfaceDestroyed(SurfaceHolder holder) {
97.
98. Log.v("Surface Destory:::", "surfaceDestroyed called");
99. }
100. @Override
101. public void onVideoSizeChanged(MediaPlayer arg0, int arg1, int arg2) {
102. // 当video⼤⼩改变时触发
103. //这个⽅法在设置player的source后⾄少触发⼀次
104. Log.v("Video Size Change", "onVideoSizeChanged called");
105.
106. }
107. @Override
108. public void onSeekComplete(MediaPlayer arg0) {
109. // seek操作完成时触发
110. Log.v("Seek Completion", "onSeekComplete called");
111.
112. }
113. @Override
114. public void onPrepared(MediaPlayer player) {
115. // 当prepare完成后,该⽅法触发,在这⾥我们播放视频
116.
117. //⾸先取得video的宽和⾼
118. vWidth = VideoWidth();
119. vHeight = VideoHeight();
120.
121. if(vWidth > Width() || vHeight > Height()){
122. //如果video的宽或者⾼超出了当前屏幕的⼤⼩,则要进⾏缩放
123. float wRatio = (float)vWidth/(Width();
124. float hRatio = (float)vHeight/(Height();
125.
126. //选择⼤的⼀个进⾏缩放
127. float ratio = Math.max(wRatio, hRatio);
128.
129. vWidth = (il((float)vWidth/ratio);
130. vHeight = (il((float)vHeight/ratio);
131.
132. //设置surfaceView的布局参数
133. surfaceView.setLayoutParams(new LinearLayout.LayoutParams(vWidth, vHeight)); 134.
135. //然后开始播放视频
136.
137. player.start();
138. }
139. }
140. @Override
141. public boolean onInfo(MediaPlayer player, int whatInfo, int extra) {
142. // 当⼀些特定信息出现或者警告时触发
143. switch(whatInfo){
144. case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:
145. break;
146. case MediaPlayer.MEDIA_INFO_METADATA_UPDATE:
147.
148. break;
149. case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
150.
151. break;
152. case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE:
153.
154. break;
155. }
156. return false;
157. }
158. @Override
159. public boolean onError(MediaPlayer player, int whatError, int extra) {
160. Log.v("Play Error:::", "onError called");
161. switch (whatError) {
162. case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
163. Log.v("Play Error:::", "MEDIA_ERROR_SERVER_DIED");
164. break;
165. case MediaPlayer.MEDIA_ERROR_UNKNOWN:
166. Log.v("Play Error:::", "MEDIA_ERROR_UNKNOWN");
167. break;
168. default:
169. break;
170. }
171. return false;
172. }
173. @Override
174. public void onCompletion(MediaPlayer player) {
175. // 当MediaPlayer播放完成后触发
176. Log.v("Play Over:::", "onComletion called");
177. this.finish();
178. }
CNKUC
看了rockplayer qq
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论