开发者

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 pla开发者_JAVA技巧ys fully.

How to achieve this.


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜