Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the MediaController visible throughout my video play

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.

like image 884
Andro Selva Avatar asked May 06 '11 10:05

Andro Selva


People also ask

How to set MediaController with VideoView in android?

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......

What is MediaController in android?

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.

How to set VideoView in android?

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.


2 Answers

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);
like image 106
Andro Selva Avatar answered Oct 19 '22 23:10

Andro Selva


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();
            }
        });
like image 24
Jossy Paul Avatar answered Oct 19 '22 23:10

Jossy Paul