視訊播放的三種方式
阿新 • • 發佈:2019-01-27
在Android中,我們有三種方式來實現視訊的播放:
1、使用其自帶的播放器。指定Action為ACTION_VIEW,Data為Uri,Type為其MIME型別。
2、使用VideoView來播放。在佈局檔案中使用VideoView結合MediaController來實現對其控制。
3、使用MediaPlayer類和SurfaceView來實現,這種方式很靈活。
1、呼叫其自帶的播放器:這個方式我試了下沒有成功,有異常。提示沒有activity用於開啟視訊
Uri uri = Uri.parse("android.resource://package_name/raw/sample.png");
這樣就可以通過Uri來使用apk中res/raw目錄下的檔案了。
- 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(
- 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;
- /**
- * 該例項中使用MediaPlayer完成播放,同時介面使用SurfaceView來實現
- *
- * 這裡我們實現MediaPlayer中很多狀態變化時的監聽器
- *
- * 使用Mediaplayer時,也可以使用MediaController類,但是需要實現MediaController.mediaController介面
- * 實現一些控制方法。
- *
- * 然後,設定controller.setMediaPlayer(),setAnchorView(),setEnabled(),show()就可以了,這裡不再實現
- * @author Administrator
- *
- */
- publicclass VideoSurfaceDemo extends Activity implements OnCompletionListener,OnErrorListener,OnInfoListener,
- OnPreparedListener, OnSeekCompleteListener,OnVideoSizeChangedListener,SurfaceHolder.Callback{
- private Display currDisplay;
- private SurfaceView surfaceView;
- private SurfaceHolder holder;
- private MediaPlayer player;
- privateint vWidth,vHeight;
- //private boolean readyToPlay = false;
- publicvoid 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
- publicvoid surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
- // 當Surface尺寸等引數改變時觸發
- Log.v("Surface Change:::", "surfaceChanged called");
- }
- @Override
- publicvoid surfaceCreated(SurfaceHolder holder) {
- // 當SurfaceView中的Surface被建立的時候被呼叫
- //在這裡我們指定MediaPlayer在當前的Surface中進行播放
- player.setDisplay(holder);
- //在指定了MediaPlayer播放的容器後,我們就可以使用prepare或者prepareAsync來準備播放了
- player.prepareAsync();
- }
- @Override
- publicvoid surfaceDestroyed(SurfaceHolder holder) {
- Log.v("Surface Destory:::", "surfaceDestroyed called");
- }
- @Override
- publicvoid onVideoSizeChanged(MediaPlayer arg0, int arg1, int arg2) {
- // 當video大小改變時觸發
- //這個方法在設定player的source後至少觸發一次
- Log.v("Video Size Change", "onVideoSizeChanged called");
- }
- @Override
- publicvoid onSeekComplete(MediaPlayer arg0) {
- // seek操作完成時觸發
- Log.v("Seek Completion", "onSeekComplete called");
- }
- @Override
- publicvoid 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
- publicboolean 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;
- }
- returnfalse;
- }
- @Override
- publicboolean 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;
- }
- returnfalse;
- }
- @Override
- publicvoid onCompletion(MediaPlayer player) {
- // 當MediaPlayer播放完成後觸發
- Log.v("Play Over:::", "onComletion called");
- this.finish();
- }
- }