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?

Ruslan
  • 1,039
  • 1
  • 9
  • 16

3 Answers3

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.

u2gilles
  • 6,888
  • 7
  • 51
  • 75
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);
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
  • Sorry, I made typo in my question, I also have tried overriding hide() method. I have fixed question text. – Ruslan May 26 '16 at 10:07
  • I have tried to put show() inside hide() - but 'back' button still doesn't work – Ruslan May 26 '16 at 10:19
  • That's the another problem check activity's onBackPressed method – Nirav Ranpara May 26 '16 at 10:21
  • I have checked onBackPressed() method - and it is never called with override. But without override it enters immediately. – Ruslan May 26 '16 at 10:26
  • then override and user finish – Nirav Ranpara May 26 '16 at 10:35
  • 1
    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. – Ruslan May 26 '16 at 10:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113018/discussion-between-nirav-ranpara-and-ruslan). – Nirav Ranpara May 26 '16 at 10:54
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.

Community
  • 1
  • 1
didinino
  • 93
  • 1
  • 8