Android三种播放视频的方式

在Android中,我们有三种方式来实现视频的播放:
1、使用其自带的播放器。指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型。
2、使用VideoView来播放。在布局文件中使用VideoView结合MediaController来实现对其控制。
3、使用MediaPlayer类和SurfaceView来实现,这种方式很灵活。

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

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来实现:

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:

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

 附件:android中音频视频开发教程(含代码).zip

posted on 2015-05-05 15:54  eustoma  阅读(29508)  评论(0编辑  收藏  举报

导航