在Android中,我们有三种方式来实现视频的播放:
1、使用其自带的播放器。指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型。
2、使用VideoView来播放。在布局文件中使用VideoView结合MediaController来实现对其控制。
3、使用MediaPlayer类和SurfaceView来实现,这种方式很灵活。
1、调用其自带的播放器:
[java] view plain copy print ?
- 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);
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来实现:
[java] view plain copy print ?
- 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();
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:
[java] view plain copy print ?
- 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;
-
- 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;
-
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.video_surface);
- surfaceView = (SurfaceView)this.findViewById(R.id.video_surface);
- holder = surfaceView.getHolder();
- holder.addCallback(this);
-
- holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- 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");
- String dataPath = Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v";
- try {
- player.setDataSource(dataPath);
- Log.v("Next:::", "surfaceDestroyed called");
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- currDisplay = this.getWindowManager().getDefaultDisplay();
- }
- @Override
- public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
- Log.v("Surface Change:::", "surfaceChanged called");
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
-
- player.setDisplay(holder);
- player.prepareAsync();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- Log.v("Surface Destory:::", "surfaceDestroyed called");
- }
- @Override
- public void onVideoSizeChanged(MediaPlayer arg0, int arg1, int arg2) {
- Log.v("Video Size Change", "onVideoSizeChanged called");
- }
- @Override
- public void onSeekComplete(MediaPlayer arg0) {
- Log.v("Seek Completion", "onSeekComplete called");
- }
- @Override
- public void onPrepared(MediaPlayer player) {
- vWidth = player.getVideoWidth();
- vHeight = player.getVideoHeight();
- if(vWidth > currDisplay.getWidth() || vHeight > currDisplay.getHeight()){
- 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.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) {
- Log.v("Play Over:::", "onComletion called");
- this.finish();
- }
- }