android视频播放 根据apidemo重写。代码如下:
package com.jamdeo.tv.livetv.player; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnBufferingUpdateListener; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnPreparedListener; import android.media.MediaPlayer.OnVideoSizeChangedListener; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class MyPlayer extends MediaPlayer implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener { public static final MyPlayer mPlayer = new MyPlayer(); private SurfaceHolder mHolder; private SurfaceView mSurfaceView; private boolean mIsVideoSizeKnown = false; private boolean mIsVideoReadyToBePlayed = false; private static final String TAG = "MyPlayer"; private int mVideoWidth; private int mVideoHeight; private String mCurrentPath; private String mPreviousPath; private MyPlayer(){ } public static MyPlayer getInstance(){ return mPlayer; } private void init(){ mPlayer.setOnBufferingUpdateListener(this); mPlayer.setOnCompletionListener(this); mPlayer.setOnPreparedListener(this); mPlayer.setOnVideoSizeChangedListener(this); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); } public void setSurfaceView(SurfaceView _surfaceView){ init(); this.mSurfaceView = _surfaceView; this.mHolder = this.mSurfaceView.getHolder(); //此方法请在setDataSource(..)之前调用,我起初就在setDataSource(...)方法后调用导致显示界面偏移 mPlayer.setDisplay(this.mHolder); } public void setPath(String _path){ this.mPreviousPath = this.mCurrentPath; this.mCurrentPath = _path; if(this.mCurrentPath.equals(this.mPreviousPath)){ return; }else{ PlayVideo(); } Log.d(TAG, "previous: " + this.mPreviousPath + "current: "+mCurrentPath); } public void PlayVideo(){ try { Log.d(TAG, "previous: " + this.mPreviousPath + "current: "+mCurrentPath); doCleanUp(); mPlayer.reset(); mPlayer.setDataSource(mCurrentPath); mPlayer.prepareAsync(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @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(); } } @Override public void onPrepared(MediaPlayer mp) { Log.d(TAG, "onPrepared called"); mIsVideoReadyToBePlayed = true; if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) { startVideoPlayback(); } } @Override public void onCompletion(MediaPlayer mp) { Log.d(TAG, "onCompletion called"); } @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { Log.d(TAG, "onBufferingUpdate percent:" + percent); } private void releaseMediaPlayer() { if (mPlayer != null) { mPlayer.release(); } } private void doCleanUp() { mVideoWidth = 0; mVideoHeight = 0; mIsVideoReadyToBePlayed = false; mIsVideoSizeKnown = false; } private void startVideoPlayback() { Log.v(TAG, "startVideoPlayback"); Log.d(TAG, "size: "+mVideoWidth +" x "+ mVideoHeight); mHolder.setFixedSize(mVideoWidth, mVideoHeight); mPlayer.start(); } }
在外部使用的时候只需要传一个SurfaceView进去然后setPath(String url),就可以直接播放了。