1、定义

private int mVideoWidth;
private int mVideoHeight;

private MediaPlayer mMediaPlayer;
private SurfaceView mPreview;
private SurfaceHolder holder;
private String path;

mPreview = (SurfaceView) findViewById(R.id.surface);
/* 得到SurfaceHolder对象 */
holder = mPreview.getHolder();
/* 设置回调函数 */
holder.addCallback(this);
/* 设置风格 */
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

2、播放

path = "/sdcard/aas.mp4";
/* 构建MediaPlayer对象 */
mMediaPlayer = new MediaPlayer();
/* 设置媒体文件路径 */
mMediaPlayer.setDataSource(path);
/* 设置通过SurfaceView来显示画面 */
mMediaPlayer.setDisplay(holder);
/* 准备 */
mMediaPlayer.prepare();
/* 设置事件监听 */
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

 

mVideoWidth = mMediaPlayer.getVideoWidth();
mVideoHeight = mMediaPlayer.getVideoHeight();
if (mVideoWidth != 0 && mVideoHeight != 0)
{
/* 设置视频的宽度和高度 */
holder.setFixedSize(mVideoWidth, mVideoHeight);
/* 开始播放 */
mMediaPlayer.start();
}