Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VideoView: how to always show MediaController while playing video

I have a very simple task - just to show playback controls while playing video on VideoView. But I can't solve it.

Here is the piece of code that I use for initializing VideoView and setting MediaController:

videoView.setVideoURI(Uri.parse(videoUrl));
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
videoView.setKeepScreenOn(true);
videoView.setOnPreparedListener(mp -> {
    progress.setVisibility(View.GONE);
    videoView.start();
    mediaController.show(0);
});

The thing is that mediaController.show(0) doesn't give any effect. Controls just show up for 3 seconds and then disappear.

Also I have tried to override MediaController's hide() method:

@Override public void hide() {}

Well, it works - controls never hide, but unfortunately hardware 'back' button stops working. Without override hardware 'back' button on first tap close media controls, on second tap - brings user to previous screen, as expected.

Are there any working solutions?

like image 409
Ruslan Avatar asked May 26 '16 10:05

Ruslan


People also ask

What is video View in android?

android.widget.VideoView. Displays a video file. The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

Which class provides methods to play and control the video player?

The basics The following classes are used to play sound and video in the Android framework: MediaPlayer. This class is the primary API for playing sound and video. AudioManager.


1 Answers

If you try to display media controler buttons as below :

    videoView.start();
    mediaController.show(0);

for some reasons show(0) is ignored.

But if you delay the call of show(0) a few milliseconds, it will work :

....
public Handler handler = new Handler();
....
videoView.start();
handler.postDelayed(
            new Runnable() {
                public void run() {
                    mediaController.show(0);
                }},
            100);

It looks a bit buggy to me and if it is design intent, it is poorly documentated.

like image 197
u2gilles Avatar answered Sep 18 '22 03:09

u2gilles