Andro Selva
Andro Selva

Reputation: 54332

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.

Upvotes: 6

Views: 9156

Answers (4)

Emad Morris Zedan
Emad Morris Zedan

Reputation: 301

VideoView videoPlayer;
MediaController mediaController;

videoPlayer = view.findViewById(R.id.videoPlayer);
Uri uri = Uri.parse(URL);
videoPlayer.setVideoURI(uri);
mediaController = new MediaController(getContext()) {
    @Override
    public void hide() {}      // on hide do nothing
};
videoPlayer.setMediaController(mediaController);
videoPlayer.requestFocus();
videoPlayer.start();

Upvotes: 1

Jossy Paul
Jossy Paul

Reputation: 1287

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();
            }
        });

Upvotes: 2

Joshua Pinter
Joshua Pinter

Reputation: 47591

More Succinct

First off, using an int timeout as a parameter in the show() method did nothing for me.

Secondly, this succinct piece of code forced the controls to stay on the screen after the first touch:

// Assumes you have a VideoView in your layout called 'video_preview'.
VideoView videoPreview = (VideoView) findViewById(R.id.video_preview);

MediaController mediaController = new MediaController(this) {                                                        
    @Override
    public void hide() {}      // Prevent hiding of controls.
};

videoPreview.setMediaController(mediaController);

Upvotes: 1

Andro Selva
Andro Selva

Reputation: 54332

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);

Upvotes: 11

Related Questions