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:
查看源码打印?
001
package demo.camera;
002
import java.io.IOException;
003
import android.app.Activity;
004
import android.media.MediaPlayer;
005
import android.media.MediaPlayer.OnCompletionListener;
006
import android.media.MediaPlayer.OnErrorListener;
007
import android.media.MediaPlayer.OnInfoListener;
008
import android.media.MediaPlayer.OnPreparedListener;
009
import android.media.MediaPlayer.OnSeekCompleteListener;
010
import android.media.MediaPlayer.OnVideoSizeChangedListener;
011
import android.os.Bundle;
012
import android.os.Environment;
013
import android.util.Log;
014
import android.view.Display;
015
import android.view.SurfaceHolder;
016
import android.view.SurfaceView;
017
import android.widget.LinearLayout;
018
/**
019
* 该实例中使用MediaPlayer完成播放,同时界面使用SurfaceView来实现
020
*
021
* 这里我们实现MediaPlayer中很多状态变化时的监听器
022
*
023
* 使用Mediaplayer时,也可以使用MediaController类,但是需要实现MediaController.mediaController接口
024
* 实现一些控制方法。
025
*
026
* 然后,设置controller.setMediaPlayer(),setAnchorView(),setEnabled(),show()就可以了,这里不再实现
027
* @author Administrator
028
*
029
*/
030
public class VideoSurfaceDemo extends Activity implements OnCompletionListener,OnErrorListener,OnInfoListener,
031
    OnPreparedListener, OnSeekCompleteListener,OnVideoSizeChangedListener,SurfaceHolder.Callback{
032
    private Display currDisplay;
033
    private SurfaceView surfaceView;
034
    private SurfaceHolder holder;
035
    private MediaPlayer player;
036
    private int vWidth,vHeight;
037
    //private boolean readyToPlay = false;
038
          
039
    public void onCreate(Bundle savedInstanceState){
040
        super.onCreate(savedInstanceState);
041
        this.setContentView(R.layout.video_surface);
042
                  
043
        surfaceView = (SurfaceView)this.findViewById(R.id.video_surface);
044
        //给SurfaceView添加CallBack监听
045
        holder = surfaceView.getHolder();
046
        holder.addCallback(this);
047
        //为了可以播放视频或者使用Camera预览,我们需要指定其Buffer类型
048
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
049
          
050
        //下面开始实例化MediaPlayer对象
051
        player = new MediaPlayer();
052
        player.setOnCompletionListener(this);
053
        player.setOnErrorListener(this);
054
        player.setOnInfoListener(this);
055
        player.setOnPreparedListener(this);
056
        player.setOnSeekCompleteListener(this);
057
        player.setOnVideoSizeChangedListener(this);
058
        Log.v("Begin:::", "surfaceDestroyed called");
059
        //然后指定需要播放文件的路径,初始化MediaPlayer
060
        String dataPath = Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v";
061
        try {
062
            player.setDataSource(dataPath);
063
            Log.v("Next:::", "surfaceDestroyed called");
064
        } catch (IllegalArgumentException e) {
065
            e.printStackTrace();
066
        } catch (IllegalStateException e) {
067
            e.printStackTrace();
068
        } catch (IOException e) {
069
            e.printStackTrace();
070
        }
071
        //然后,我们取得当前Display对象
072
        currDisplay = this.getWindowManager().getDefaultDisplay();
073
    }
074
      
075
    @Override
076
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
077
        // 当Surface尺寸等参数改变时触发
078
        Log.v("Surface Change:::", "surfaceChanged called");
079
    }
080
    @Override
081
    public void surfaceCreated(SurfaceHolder holder) {
082
        // 当SurfaceView中的Surface被创建的时候被调用
083
        //在这里我们指定MediaPlayer在当前的Surface中进行播放
084
        player.setDisplay(holder);
085
        //在指定了MediaPlayer播放的容器后,我们就可以使用prepare或者prepareAsync来准备播放了
086
        player.prepareAsync();
087
          
088
    }
089
    @Override
090
    public void surfaceDestroyed(SurfaceHolder holder) {
091
          
092
        Log.v("Surface Destory:::", "surfaceDestroyed called");
093
    }
094
    @Override
095
    public void onVideoSizeChanged(MediaPlayer arg0, int arg1, int arg2) {
096
        // 当video大小改变时触发
097
        //这个方法在设置player的source后至少触发一次
098
        Log.v("Video Size Change", "onVideoSizeChanged called");
099
          
100
    }
101
    @Override
102
    public void onSeekComplete(MediaPlayer arg0) {
103
        // seek操作完成时触发
104
        Log.v("Seek Completion", "onSeekComplete called");
105
          
106
    }
107
    @Override
108
    public void onPrepared(MediaPlayer player) {
109
        // 当prepare完成后,该方法触发,在这里我们播放视频
110
          
111
        //首先取得video的宽和高
112
        vWidth = player.getVideoWidth();
113
        vHeight = player.getVideoHeight();
114
          
115
        if(vWidth > currDisplay.getWidth() || vHeight > currDisplay.getHeight()){
116
            //如果video的宽或者高超出了当前屏幕的大小,则要进行缩放
117
            float wRatio = (float)vWidth/(float)currDisplay.getWidth();
118
            float hRatio = (float)vHeight/(float)currDisplay.getHeight();
119
              
120
            //选择大的一个进行缩放
121
            float ratio = Math.max(wRatio, hRatio);
122
              
123
            vWidth = (int)Math.ceil((float)vWidth/ratio);
124
            vHeight = (int)Math.ceil((float)vHeight/ratio);
125
              
126
            //设置surfaceView的布局参数
127
            surfaceView.setLayoutParams(new LinearLayout.LayoutParams(vWidth, vHeight));
128
              
129
            //然后开始播放视频
130
              
131
            player.start();
132
        }
133
    }
134
    @Override
135
    public boolean onInfo(MediaPlayer player, int whatInfo, int extra) {
136
        // 当一些特定信息出现或者警告时触发
137
        switch(whatInfo){
138
        case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:
139
            break;
140
        case MediaPlayer.MEDIA_INFO_METADATA_UPDATE:  
141
            break;
142
        case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
143
            break;
144
        case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE: 
145
            break;
146
        }
147
        return false;
148
    }
149
    @Override
150
    public boolean onError(MediaPlayer player, int whatError, int extra) {
151
        Log.v("Play Error:::", "onError called");
152
        switch (whatError) {
153
        case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
154
            Log.v("Play Error:::", "MEDIA_ERROR_SERVER_DIED");
155
            break;
156
        case MediaPlayer.MEDIA_ERROR_UNKNOWN:
157
            Log.v("Play Error:::", "MEDIA_ERROR_UNKNOWN");
158
            break;
159
        default:
160
            break;
161
        }
162
        return false;
163
    }
164
    @Override
165
    public void onCompletion(MediaPlayer player) {
166
        // 当MediaPlayer播放完成后触发
167
        Log.v("Play Over:::", "onComletion called");
168
        this.finish();
169
    }
170
}

你可能感兴趣的:(android)