- <span style="line-height: 1.5;">public class MediaPlayerDemo_Video extends Activity implements</span>
- OnBufferingUpdateListener, OnCompletionListener,
- OnPreparedListener, OnVideoSizeChangedListener,
- SurfaceHolder.Callback {
- private static final String TAG = "MediaPlayerDemo";
- private int mVideoWidth;
- private int mVideoHeight;
- private MediaPlayer mMediaPlayer;
- private SurfaceView mPreview;
- private SurfaceHolder holder;
- private Bundle extras;
- private static final String MEDIA = "media";
- private static final int LOCAL_AUDIO = 1;
- private static final int STREAM_AUDIO = 2;
- private static final int RESOURCES_AUDIO = 3;
- private static final int LOCAL_VIDEO = 4;
- private static final int STREAM_VIDEO = 5;
- private boolean mIsVideoSizeKnown = false;
- private boolean mIsVideoReadyToBePlayed = false;
- /**
- *
- * Called when the activity is first created.
- */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.mediaplayer_2);
- mPreview = (SurfaceView) findViewById(R.id.surface);
- holder = mPreview.getHolder();
- holder.addCallback(this);
- holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- extras = getIntent().getExtras();
- }
- private void playVideo(Integer Media,String path) {
- doCleanUp();
- try {
- switch (Media) {
- case LOCAL_VIDEO:
- if (path.equals("")) {
- // Tell the user to provide a media file URL.
- Toast
- .makeText(
- MediaPlayerDemo_Video.this,"文件名不能为空",
- Toast.LENGTH_LONG).show();
- }
- break;
- case STREAM_VIDEO:
- if (path.equals("")) {
- // Tell the user to provide a media file URL.
- Toast .makeText(
- MediaPlayerDemo_Video.this,"文件名不能为空",
- Toast.LENGTH_LONG).show();
- }
- break;
- }
- // 创建媒体播放器和设置监听器
- mMediaPlayer = new MediaPlayer();
- mMediaPlayer.setDataSource(path);
- mMediaPlayer.setDisplay(holder);
- //准备播放
- mMediaPlayer.prepare();
- mMediaPlayer.setOnBufferingUpdateListener(this);
- mMediaPlayer.setOnCompletionListener(this);
- mMediaPlayer.setOnPreparedListener(this);
- mMediaPlayer.setOnVideoSizeChangedListener(this);
- mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- } catch (Exception e) {
- Log.e(TAG, "error: " + e.getMessage(), e);
- }
- }
- //缓冲更新提示
- public void onBufferingUpdate(MediaPlayer arg0, int percent) {
- Log.d(TAG, "onBufferingUpdate percent:" + percent);
- }
- //播放器做好了准备
- public void onPrepared(MediaPlayer mediaplayer) {
- Log.d(TAG, "onPrepared called");
- mIsVideoReadyToBePlayed = true;
- if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
- startVideoPlayback();
- }
- }
- //播放完成
- public void onCompletion(MediaPlayer arg0) {
- Log.d(TAG, "onCompletion called");
- }
- //暂停
- @Override
- protected void onPause() {
- super.onPause();
- releaseMediaPlayer();
- doCleanUp();
- }
- //销毁
- @Override
- protected void onDestroy() {
- super.onDestroy();
- releaseMediaPlayer();
- doCleanUp();
- }
- //释放内存
- private void releaseMediaPlayer() {
- if (mMediaPlayer != null) {
- mMediaPlayer.release();
- mMediaPlayer = null;
- }
- }
- //重置
- private void doCleanUp() {
- mVideoWidth = 0;
- mVideoHeight = 0;
- mIsVideoReadyToBePlayed = false;
- mIsVideoSizeKnown = false;
- }
- @Override
- public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
- Log.v(TAG, "onVideoSizeChanged called");
- if (width == 0 || height == 0) {
- Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
- return;
- }
- mIsVideoSizeKnown = true;
- mVideoWidth = width;
- mVideoHeight = height;
- if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
- startVideoPlayback();
- }
- }
- //开始播放
- private void startVideoPlayback() {
- Log.v(TAG, "startVideoPlayback");
- holder.setFixedSize(mVideoWidth, mVideoHeight);
- mMediaPlayer.start();
- }
- public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
- Log.d(TAG, "surfaceChanged called");
- }
- public void surfaceDestroyed(SurfaceHolder surfaceholder) {
- Log.d(TAG, "surfaceDestroyed called");
- }
- public void surfaceCreated(SurfaceHolder holder) {
- Log.d(TAG, "surfaceCreated called");
- //从extras中获得媒体类型和媒体文件的路径,并开始播放
- playVideo(extras.getInt(MEDIA),extras.getString("path"));
- }
- }
复制代码 |
|