在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) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { 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的布局參數