快捷搜索:

Android多媒体学习十二:Android中Video的三种播放方

在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) {// TODO Auto-generated catch block

e.printStackTrace();} catch (IllegalStateException e) {

// TODO Auto-generated catch blocke.printStackTrace();

} catch (IOException e) {// TODO Auto-generated catch block

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");}

@Overridepublic 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");}

@Overridepublic 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();

}}

@Overridepublic 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;}

@Overridepublic 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();

}}

应用的结构文件都很简单,就不贴了。实际测试时,发明在模拟器中会呈现卡在第一帧的征象,具网上牛人们说,这是模拟器自身的问题。

在真机上测试就可以。

您可能还会对下面的文章感兴趣: