在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的布局參數 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(); } }