VideoView: how to always show MediaController while playing video
Asked Answered
V

3

8

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?

Viral answered 26/5, 2016 at 10:2 Comment(0)
S
9

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.

Semiaquatic answered 6/2, 2017 at 3:41 Comment(0)
M
4

MediaController hide in 3 seconds by default, so you need to override hide() to display MediaController

videoView.setMediaController(new MediaController(this) {
    @Override
    public void hide()
    {
       mediaController.show();
    }

    }); 

videoView.setMediaController(mc);
Mirnamirror answered 26/5, 2016 at 10:5 Comment(7)
Sorry, I made typo in my question, I also have tried overriding hide() method. I have fixed question text.Viral
I have tried to put show() inside hide() - but 'back' button still doesn't workViral
That's the another problem check activity's onBackPressed methodMirnamirror
I have checked onBackPressed() method - and it is never called with override. But without override it enters immediately.Viral
then override and user finishMirnamirror
Sorry, what did you mean? I already have tried to override onBackPressed() - but it is never called if I override MediaController's hide() method. If I don't override MediaController's hide() method then onBackPressed() it called on 'back' button press.Viral
Let us continue this discussion in chat.Mirnamirror
R
0

In addition to u2gilles's answer, I encountered that the delay was too short resulting "play" symbol being displayed instead of the "pause" symbol.

Modify the handler's delay to ~250ms to fix it.

Runlet answered 1/4, 2017 at 22:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.