It's quite simple to add media controller in a media player. Make your activity implement MediaPlayerControl and add unimplemented methods. Code is as below:
I am using code from Api demos from here http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html
public class ActivityVedioPlay extends Activity implements
OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
OnVideoSizeChangedListener, SurfaceHolder.Callback, MediaPlayerControl {
.....
private MediaPlayer mMediaPlayer;
private MediaController mcontroller;
private Handler handler = new Handler();
.....
@Override
public boolean onTouchEvent(MotionEvent event) {
/*
* the MediaController will hide after 3 seconds - tap the screen to
* make it appear again
*/
mcontroller.show();
return false;
}
......
private void playVideo() {
doCleanUp();
try {
path = getIntent().getStringExtra("url");
if (path == "") {
Toast.makeText(ActivityVedioPlay.this, "URL Not found",
Toast.LENGTH_LONG).show();
}
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(path);
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mcontroller = new MediaController(this);
} catch (Exception e) {
e.printStackTrace();
}
}
......
public void onPrepared(MediaPlayer mediaplayer) {
Log.d(TAG, "onPrepared called");
mIsVideoReadyToBePlayed = true;
if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
startVideoPlayback();
}
mcontroller.setMediaPlayer(this);
mcontroller.setAnchorView(findViewById(R.id.mediaplayer_surfaceview_container));
handler.post(new Runnable() {
public void run() {
mcontroller.setEnabled(true);
mcontroller.show();
}
});
}
//mediacontroller implemented methods
public void start() {
mMediaPlayer.start();
}
public void pause() {
mMediaPlayer.pause();
}
public int getDuration() {
return mMediaPlayer.getDuration();
}
public int getCurrentPosition() {
return mMediaPlayer.getCurrentPosition();
}
public void seekTo(int i) {
mMediaPlayer.seekTo(i);
}
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
public int getBufferPercentage() {
return 0;
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…