在Android中,我們有三種方式來實現視訊的播放:
1、使用其自帶的播放器。指定Action為ACTION_VIEW,Data為Uri,Type為其MIME型別。
2、使用VideoView來播放。在佈局檔案中使用VideoView結合MediaController來實現對其控制。
3、使用MediaPlayer類和SurfaceView來實現,這種方式很靈活。
1、呼叫其自帶的播放器:
1 2 3 4 5 6 |
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來實現:
1 2 3 4 5 6 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
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(); } } |