在Android中,我们有三种方式来实现视频的播放。
1、使用其自带的播放器。指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型。
2、使用VideoView来播放。在布局文件中使用VideoView结合MediaController来实现对其控制。
3、使用MediaPlayer类和SurfaceView来实现。这种方式最灵活,也最复杂。
下面给出这三种方式,
1、调用其自带的播放器:
- Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v");
- //调用系统自带的播放器
- Intent intent = new Intent(Intent.ACTION_VIEW);
- Log.v("URI:::::::::", uri.toString());
- intent.setDataAndType(uri, "video/mp4");
- startActivity(intent);
2、使用VideoView来实现:
- Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v");
- VideoView videoView = (VideoView)this.findViewById(R.id.video_view);
- videoView.setMediaController(new MediaController(this));
- videoView.setVideoURI(uri);
- //videoView.start();
- videoView.requestFocus();
3、使用MediaPlayer:
- package demo.camera;
- import java.io.IOException;
- import android.app.Activity;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.media.MediaPlayer.OnErrorListener;
- import android.media.MediaPlayer.OnInfoListener;
- import android.media.MediaPlayer.OnPreparedListener;
- import android.media.MediaPlayer.OnSeekCompleteListener;
- import android.media.MediaPlayer.OnVideoSizeChangedListener;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.Display;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.widget.LinearLayout;
- /**
- * 该实例中使用MediaPlayer完成播放,同时界面使用SurfaceView来实现
- *
- * 这里我们实现MediaPlayer中很多状态变化时的监听器
- *
- * 使用Mediaplayer时,也可以使用MediaController类,但是需要实现MediaController.mediaController接口
- * 实现一些控制方法。
- *
- * 然后,设置controller.setMediaPlayer(),setAnchorView(),setEnabled(),show()就可以了,这里不再实现
- * @author Administrator
- *
- */
- public class VideoSurfaceDemo extends Activity implements OnCompletionListener,OnErrorListener,OnInfoListener,
- OnPreparedListener, OnSeekCompleteListener,OnVideoSizeChangedListener,SurfaceHolder.Callback{
- private Display currDisplay;
- private SurfaceView surfaceView;
- private SurfaceHolder holder;
- private MediaPlayer player;
- private int vWidth,vHeight;
- //private boolean readyToPlay = false;
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.video_surface);
- surfaceView = (SurfaceView)this.findViewById(R.id.video_surface);
- //给SurfaceView添加CallBack监听
- holder = surfaceView.getHolder();
- holder.addCallback(this);
- //为了可以播放视频或者使用Camera预览,我们需要指定其Buffer类型
- holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- //下面开始实例化MediaPlayer对象
- player = new MediaPlayer();
- player.setOnCompletionListener(this);
- player.setOnErrorListener(this);
- player.setOnInfoListener(this);
- player.setOnPreparedListener(this);
- player.setOnSeekCompleteListener(this);
- player.setOnVideoSizeChangedListener(this);
- Log.v("Begin:::", "surfaceDestroyed called");
- //然后指定需要播放文件的路径,初始化MediaPlayer
- String dataPath = Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v";
- try {
- player.setDataSource(dataPath);
- Log.v("Next:::", "surfaceDestroyed called");
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //然后,我们取得当前Display对象
- currDisplay = this.getWindowManager().getDefaultDisplay();
- }
- @Override
- public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
- // 当Surface尺寸等参数改变时触发
- Log.v("Surface Change:::", "surfaceChanged called");
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // 当SurfaceView中的Surface被创建的时候被调用
- //在这里我们指定MediaPlayer在当前的Surface中进行播放
- player.setDisplay(holder);
- //在指定了MediaPlayer播放的容器后,我们就可以使用prepare或者prepareAsync来准备播放了
- player.prepareAsync();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- Log.v("Surface Destory:::", "surfaceDestroyed called");
- }
- @Override
- public void onVideoSizeChanged(MediaPlayer arg0, int arg1, int arg2) {
- // 当video大小改变时触发
- //这个方法在设置player的source后至少触发一次
- Log.v("Video Size Change", "onVideoSizeChanged called");
- }
- @Override
- public void onSeekComplete(MediaPlayer arg0) {
- // seek操作完成时触发
- Log.v("Seek Completion", "onSeekComplete called");
- }
- @Override
- public void onPrepared(MediaPlayer player) {
- // 当prepare完成后,该方法触发,在这里我们播放视频
- //首先取得video的宽和高
- vWidth = player.getVideoWidth();
- vHeight = player.getVideoHeight();
- if(vWidth > currDisplay.getWidth() || vHeight > currDisplay.getHeight()){
- //如果video的宽或者高超出了当前屏幕的大小,则要进行缩放
- float wRatio = (float)vWidth/(float)currDisplay.getWidth();
- float hRatio = (float)vHeight/(float)currDisplay.getHeight();
- //选择大的一个进行缩放
- float ratio = Math.max(wRatio, hRatio);
- vWidth = (int)Math.ceil((float)vWidth/ratio);
- vHeight = (int)Math.ceil((float)vHeight/ratio);
- //设置surfaceView的布局参数
- surfaceView.setLayoutParams(new LinearLayout.LayoutParams(vWidth, vHeight));
- //然后开始播放视频
- player.start();
- }
- }
- @Override
- public boolean onInfo(MediaPlayer player, int whatInfo, int extra) {
- // 当一些特定信息出现或者警告时触发
- switch(whatInfo){
- case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:
- break;
- case MediaPlayer.MEDIA_INFO_METADATA_UPDATE:
- break;
- case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
- break;
- case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE:
- break;
- }
- return false;
- }
- @Override
- public boolean onError(MediaPlayer player, int whatError, int extra) {
- Log.v("Play Error:::", "onError called");
- switch (whatError) {
- case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
- Log.v("Play Error:::", "MEDIA_ERROR_SERVER_DIED");
- break;
- case MediaPlayer.MEDIA_ERROR_UNKNOWN:
- Log.v("Play Error:::", "MEDIA_ERROR_UNKNOWN");
- break;
- default:
- break;
- }
- return false;
- }
- @Override
- public void onCompletion(MediaPlayer player) {
- // 当MediaPlayer播放完成后触发
- Log.v("Play Over:::", "onComletion called");
- this.finish();
- }
- }
使用的布局文件都很简单,就不贴了。实际测试时,发现在模拟器中会出现卡在第一帧的现象,具网上牛人们说,这是模拟器自身的问题。
在真机上测试就可以。