I am using MediaController in my app. But unfortunately the MediaController disappears after a time period of 3 secs. But I want my MediaController to be visible until my video plays fully.
How to achieve this.
Just define the MediaController object in on create and then add the view to that otherwise the error will like adding a view in a null object reference......
android.widget.MediaController. A view containing controls for a MediaPlayer. Typically contains the buttons like "Play/Pause", "Rewind", "Fast Forward" and a progress slider. It takes care of synchronizing the controls with the state of the MediaPlayer. The way to use this class is to instantiate it programmatically.
Step 2: Open res -> layout -> xml (or) main. xml and add following code : In this step we open an xml file and add the code to display a VideoView in our activity. In this step we open MainActivity and add the code to initiate the video view and create an object of MediaController to control the video playback.
By default the MediaController hides itself after 3 secs of time. In order to make it visible throughout our video playback we will have to override the hide() of MediaController. I have given the code snippet below.
final MediaController mc = new MediaController(this);
video.setMediaController(new MediaController(this) {
@Override
public void hide()
{
mc.show();
}
});
video.setMediaController(mc);
For stop hiding the MediaController we can make a new Mediacontroller by extending the base class. Then we can disable the hide method by simply overriding it. For getting the actual hide functionality, we can fetch the hide() method in base class. We can hide the Mediacontroller after playback is completed using that. Here is the code for MediaController:
public class MediaController_2 extends MediaController{
public MediaController_2(Context context) {
super(context);
}
public void hide() {
}
public void hidecontroller() {
super.hide();
}
}
Now the mediacontroller won't be hiding even after the completion of the playback. For hiding the controllers after completing playback we can use OnCompletionListener.
MediaController_2 mediaController = new MediaController_2(getActivity());
mediaPlayer.prepare();
mediaPlayer.start();
mediaController.show(0);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaController.hidecontroller();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With