/**
* 网络视频播放
*
* openCore
*
* stagefright
*
* @time 下午05:15:41
* @author retacn yue
* @Email
[email protected]
*/
@SuppressWarnings("unused")
public class StreamVideoActivity extends Activity implements //
SurfaceHolder.Callback, //
OnErrorListener,//
OnCompletionListener,//
OnInfoListener,//
OnPreparedListener,//
OnSeekCompleteListener, //
OnVideoSizeChangedListener, //
OnBufferingUpdateListener,//
MediaPlayerControl, OnTouchListener {//
SurfaceView surfaceView;
TextView txv_status;
LinearLayout main_view;
MediaPlayer mediaPlayer;
SurfaceHolder surfaceHolder;
Display currentDisplay;
MediaController controller;
int videoH = 0;
int videoW = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stream_video);
findView();
init();
}
// rtsp://media2.tripsmarter.com/LiveTV/BTV/
// rtsp://202.38.64.28/broadcast/tvserver/encoder/tv1
// rtsp://v.starv.tv/later.3gp
/**
* 初始化方法
*
*/
private void init() {
String filePath = " rtsp://v.starv.tv/later.3gp";
// String filePath = Environment.getExternalStorageDirectory().getPath()
// + "/kiss.mp4";
try {
mediaPlayer.setDataSource(filePath);
} catch (IllegalArgumentException e) {
e.printStackTrace();
Log.i("tag", "" + e.getMessage());
finish();
} catch (IllegalStateException e) {
e.printStackTrace();
Log.i("tag", "" + e.getMessage());
finish();
} catch (IOException e) {
e.printStackTrace();
Log.i("tag", "" + e.getMessage());
finish();
}
txv_status.setText("MediaPlayer Set Datasource!");
currentDisplay = getWindowManager().getDefaultDisplay();
controller = new MediaController(this);
}
/**
* 实例化控件
*/
private void findView() {
main_view = (LinearLayout) this.findViewById(R.id.lay_mview);
main_view.setOnTouchListener(this);
txv_status = (TextView) this.findViewById(R.id.txt_video_status);
surfaceView = (SurfaceView) this.findViewById(R.id.sfv_stream_view);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
txv_status.setText("MediaPlayer is created!");
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnVideoSizeChangedListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
}
/********************************************/
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i("tag", "===surfaceCreated()===");
mediaPlayer.setDisplay(surfaceHolder);
try {
// TODO
mediaPlayer.prepareAsync();
} catch (IllegalStateException e) {
Log.i("tag", "mediaplay prepare: " + e.getMessage());
e.printStackTrace();
finish();
}
txv_status.setText("MediaPlayer Preparing");
}
@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()===");
txv_status.setText("MediaPlayer playback completed");
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
txv_status.setText("MediaPlayer Error!");
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) {
txv_status.setText("MediaPlayer onInfo!");
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) {
Log.i("tag", "===onPrepared()===");
txv_status.setText("MediaPlayer prepared !");
// 取得播放器大小
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) ratioH);
} else {
videoH = (int) Math.ceil((float) videoH / (float) ratioW);
videoW = (int) Math.ceil((float) videoW / (float) ratioW);
}
}
}
surfaceView.setLayoutParams(new LinearLayout.LayoutParams(videoW, videoH));
controller.setMediaPlayer(this);
controller.setAnchorView(main_view);
controller.setEnabled(true);
controller.show();
mp.start();
txv_status.setText("MediaPlayer started !");
}
@Override
public void onSeekComplete(MediaPlayer mp) {
Log.i("tag", "===onSeekComplete()===");
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
Log.i("tag", "===onVideoSizeChanged()===");
// 取得播放器大小
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) ratioH);
} else {
videoH = (int) Math.ceil((float) videoH / (float) ratioW);
videoW = (int) Math.ceil((float) videoW / (float) ratioW);
}
}
}
surfaceView.setLayoutParams(new LinearLayout.LayoutParams(videoW, videoH));
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
txv_status.setText("MediaPlayer buffering: " + percent + "%");
Log.i("tag", "MediaPlayer buffering: " + percent + "%");
}
/****************************************************/
@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;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (controller.isShowing()) {
controller.hide();
} else {
controller.show();
}
return false;
}
}