使用surfaceView制作视频播放器

package com.example.myvideoviewandsurfaceview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("SdCardPath")
public class MainActivity extends Activity implements SurfaceHolder.Callback {
	private TextView typeText;
	private static final String TAG = "HIPPO_MediaPlayer";

	private MediaPlayer mMediaPlayer;
	private SurfaceView mSurfaceView;
	private SurfaceHolder mSurfaceHolder;
	/** 播放 */
	private ImageButton mPlay;
	/** 暂停 */
	private ImageButton mPause;
	/** 复位 */
	private ImageButton mReset;
	/** 停止 */
	private ImageButton mStop;
	/** 是否暂停 */
	private boolean bIsPaused = false;
	/** {@link MediaPlayer}是否被释放的flag */
	private boolean bIsRelased = false;
	/** 测试视频地址 */
	private String strVideoPath = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		/* 判断SD卡是否存在 */
		if (!checkSDCard()) {
			mMakeTextToast(getResources().getString(R.string.str_err_nosd),
					true);
		}
		/* 初始化控件 */
		init();
		/* 设置按钮监听 */
		setListener();
	}

	/** 初始化控件 */
	private void init() {
		mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);

		typeText = (TextView) findViewById(R.id.text_view);
		mPlay = (ImageButton) findViewById(R.id.play);
		mPause = (ImageButton) findViewById(R.id.pause);
		mStop = (ImageButton) findViewById(R.id.stop);
		mReset = (ImageButton) findViewById(R.id.reset);
		getWindow().setFormat(PixelFormat.UNKNOWN);
		mSurfaceHolder = mSurfaceView.getHolder();
		strVideoPath = "/sdcard/VID_20151108_175248.mp4";
	}

	/** 设置按钮监听 */
	private void setListener() {
		/* 添加返回监听 */
		mSurfaceHolder.addCallback(this);
		mPlay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkSDCard()) {
					/* 播放视频 */
					playVideo(strVideoPath);
				}
			}
		});

		mPause.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mMediaPlayer != null) {// 播放类不为空
					if (bIsRelased == false) {// 没被释放
						if (bIsPaused == false) {// 没有暂停
							mMediaPlayer.pause();// 暂停
							bIsPaused = true;// 使暂停状态为true
							typeText.setText(R.string.str_pause);// 更改文字显示
						} else if (bIsPaused == true) {// 已经暂停
							mMediaPlayer.start();// 开始
							bIsPaused = false;// 使暂停状态为false
							typeText.setText(R.string.str_play);// 更改文字显示
						}
					}
				}
			}
		});

		mReset.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkSDCard()) {
					if (bIsRelased == false) {// 没有被回收
						if (mMediaPlayer != null) {// 移动到开始位置
							mMediaPlayer.seekTo(0);
						}
					}
				}
			}
		});
		mStop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkSDCard()) {
					if (mMediaPlayer != null) {// 非空
						if (bIsRelased == false) {// 没被回收
							mMediaPlayer.stop();// 停止
							mMediaPlayer.release();// 清空
							bIsRelased = true;// 设置为以回收
							typeText.setText(R.string.str_stop);// 设置停止
						}
					}
				}
			}
		});
	}

	/** 播放视频 */
	protected void playVideo(String path) {
		mMediaPlayer = new MediaPlayer();
		mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		mMediaPlayer.setDisplay(mSurfaceHolder);
		try {
			// 设置数据源
			mMediaPlayer.setDataSource(path);
		} catch (Exception e) {
			typeText.setText("setDataSource Exceeption:" + e.toString());
			e.printStackTrace();
		}
		try {
			// 初始化
			mMediaPlayer.prepare();
		} catch (Exception e) {
			typeText.setText("prepare Exception:" + e.toString());
		}
		// 开始播放
		mMediaPlayer.start();
		bIsRelased = false;
		typeText.setText(R.string.str_play);
		// 播放完成监听
		mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				typeText.setText(R.string.str_stop);
			}
		});
	}

	/** 屏幕提醒方法 */
	private void mMakeTextToast(String string, boolean isLong) {
		if (isLong) {
			Toast.makeText(MainActivity.this, string, Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT)
					.show();
		}
	}

	private boolean checkSDCard() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Log.i(TAG, "surfaceCreated");
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		Log.i(TAG, "surfaceChanged");
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.i(TAG, "surfaceDestroyed");
	}
}

代码如上图所示,基本都添加了注释,比较容易看懂,界面如下,由于搭建比较简单,就不上代码了,string中就是一些暂停、停止、播放之类与状态有关的字,所以也不写了。

使用surfaceView制作视频播放器_第1张图片

你可能感兴趣的:(使用surfaceView制作视频播放器)