I have an Android application that uses a VideoView to play videos and I am also using a media controller.
My problem is I am unable to prevent the media controller from disappearing while the video is playing and i would also like to make the media controller visible before the video starts so that the user can use the media controller to initially start the video. I have tried using using MediaController.show(0), but that didn't solve my problem. Here is my code:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
MediaController mc = new MediaController(this);
mVideoView.setMediaController(mc);
mPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
}
private void playVideo() {
try {
final String path = "http://toop.voxelperfect.net/video6.mp4";
System.out.println("path "+path);
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(SampleActivity.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
System.out.println("else ");
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
System.out.println("mVideoView.start() ");
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
current = path;
MediaController mc = new MediaController(this);
mVideoView.setMediaController(mc);
mVideoView.setVideoURI(Uri.parse(path));
mVideoView.start();
mVideoView.requestFocus();
mc.show(0);
}
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage());
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
I solved my problem by creating a nested class that implements the MediaController and overrided the hide method.