/**
*
* 自定义播放器
*
* @time 下午05:15:41
* @author retacn yue
* @Email [email protected]
*/
public class CustomVidoePlayActivity extends Activity implements Callback,//
OnCompletionListener,//
OnErrorListener, //
OnInfoListener, //
OnPreparedListener, //
OnSeekCompleteListener,//
OnVideoSizeChangedListener,//
MediaPlayerControl {//
private SurfaceView myVideo;
private Display currentDisplay;
int videoW = 0;
int videoH = 0;
MediaPlayer mediaPlayer;
SurfaceHolder surfaceHolder;
MediaController mediaController;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("tag", "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_videoplay);
findView();
init();
}
/**
* 初始化方法
*/
private void init() {
Log.i("tag", "init()");
String filePath = Environment.getExternalStorageDirectory().getPath() + "/kiss.mp4";
surfaceHolder = myVideo.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
// 设置监听
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnVideoSizeChangedListener(this);
try {
mediaPlayer.setDataSource(filePath);
} catch (IllegalArgumentException e) {
Log.i("tag", "cach message: " + e.getMessage());
finish();
} catch (IllegalStateException e) {
Log.i("tag", "cach message: " + e.getMessage());
finish();
} catch (IOException e) {
Log.i("tag", "cach message: " + e.getMessage());
finish();
}
// 取得当前屏幕大小
currentDisplay = getWindowManager().getDefaultDisplay();
mediaController = new MediaController(this);
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(this.findViewById(R.id.lay_main));
mediaController.setEnabled(true);
}
/**
* 实例化控件
*/
private void findView() {
Log.i("tag", "findView()");
myVideo = (SurfaceView) this.findViewById(R.id.sfv_show);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mediaController.isShowing()) {
mediaController.hide();
} else {
mediaController.show();
}
return false;
}
/****************************************************/
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i("tag", "===surfaceCreated()===");
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
Log.i("tag", "mediaplay prepare: " + e.getMessage());
e.printStackTrace();
finish();
} catch (IOException e) {
Log.i("tag", "mediaplay parpare: " + e.getMessage());
e.printStackTrace();
finish();
}
}
@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()===");
}
/*******************************************************/
@Override
public void onCompletion(MediaPlayer mp) {
Log.i("tag", "===onCompletion()===");
finish();
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.i("tag", "===onError()===");
if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
Log.i("tag", "Media Error,Server Died! " + extra);
} else if (what == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
Log.i("tag", "Media Error, Unknow Error! " + extra);
}
return false;
}
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING) {
Log.i("tag", "Media info,Media info bad interleaveing " + extra);
} else if (what == MediaPlayer.MEDIA_INFO_METADATA_UPDATE) {
Log.i("tag", "Media info,Media info metadata update " + extra);
} else if (what == MediaPlayer.MEDIA_INFO_NOT_SEEKABLE) {
Log.i("tag", "Media info,Media info not_seekable " + extra);
} else if (what == MediaPlayer.MEDIA_INFO_UNKNOWN) {
Log.i("tag", "Media info,Media info unknown " + extra);
} else if (what == MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Log.i("tag", "Media info,Media info video track lagging " + extra);
}
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
// 取得播放器大小
videoH = mp.getVideoHeight();
videoW = mp.getVideoWidth();
if (videoH > currentDisplay.getHeight() || videoW > currentDisplay.getWidth()) {
// 缩放
float ratioH = (float) videoH / (float) currentDisplay.getHeight();
float ratioW = (float) videoW / (float) currentDisplay.getWidth();
if (ratioH > 1 || ratioW > 1) {
if (ratioH > ratioW) {
videoH = (int) Math.ceil((float) videoH / (float) ratioH);
videoW = (int) Math.ceil((float) videoW / (float) ratioW);
} else {
}
}
}
myVideo.setLayoutParams(new LinearLayout.LayoutParams(videoW, videoH));
mp.start();
//TODO
mediaController.show();
}
@Override
public void onSeekComplete(MediaPlayer mp) {
Log.i("tag", "===onSeekComplete()===");
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
Log.i("tag", "===onVideoSizeChanged()===");
}
/***********************************************/
@Override
public void start() {
mediaPlayer.start();
}
@Override
public void pause() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
@Override
public int getDuration() {
return mediaPlayer.getDuration();
}
@Override
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
@Override
public void seekTo(int pos) {
mediaPlayer.seekTo(pos);
}
@Override
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
@Override
public int getBufferPercentage() {
return 0;
}
@Override
public boolean canPause() {
return true;
}
@Override
public boolean canSeekBackward() {
return true;
}
@Override
public boolean canSeekForward() {
return true;
}
}