24

I am using mediacontroller in my app, but it shows only for 3 seconds. I have searched a lot, but in every document I see only the show function, set time out, but it has no effect. How can I always show mediacontroller?

I have tested show(0), but it had no effect.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Bytecode
  • 6,551
  • 16
  • 54
  • 101

10 Answers10

27

You can extend the MediaController class and programmatically set an instance of it to a VideoView class:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.MediaController;

public class MyMediaController extends MediaController {
    public MyMediaController(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyMediaController(Context context, boolean useFastForward) {
        super(context, useFastForward);
    }

    public MyMediaController(Context context) {
        super(context);
    }

    @Override
    public void show(int timeout) {
        super.show(0);
    }

}

Here's the usage:

VideoView myVideoView = (VideoView) findViewById(R.id.my_video_view);
MediaController mc = new MyMediaController(myVideoView.getContext());
mc.setMediaPlayer(myVideoView);
myVideoView.setMediaController(mc);
pcting
  • 1,286
  • 14
  • 20
13

You can create anonymous class inline and override certain methods. You need to override the hide method and do nothing in there. You also need to override the dispatchKeyEvent method to check for back key press and call the super.hide(). Otherwise on back press the controller wont hide and the activity cannot be closed.

 mediaController = new MediaController(this){
        @Override
        public void hide() {
            // TODO Auto-generated method stub
            //do nothing
        }

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {

            if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

                if (mediaPlayer != null) {
                    mediaPlayer.reset();
                    mediaPlayer.release();
                    mediaPlayer = null;
                }
                super.hide();
                Activity a = (Activity)getContext();
                a.finish();

            }
        return true;
        }
    };
mkhan
  • 621
  • 4
  • 10
  • 1
    My volume controls (on screen and device buttons) don't work at all when I override these functions. – blackcompe Aug 12 '14 at 13:06
  • 1
    Code in last post at (http://stackoverflow.com/questions/23338989/android-overriding-back-button-causes-volume-buttons-stop-working) works perfectly. – blackcompe Aug 12 '14 at 13:12
  • Only `return true` if `event.getKeyCode() == KeyEvent.KEYCODE_BACK`. Otherwise `return false` and then the other device buttons will continue to work – Chris Read Mar 07 '17 at 14:09
  • Also, add `mediaController.hide()` before finishing the activity – Chris Read Mar 07 '17 at 14:11
12

You can also create an anonymous class inline and override the hide method there instead of having to create a whole new class for it:

mediaController = new MediaController(this) {
    @Override
    public void hide() {
    //Do not hide.
    }
};
Gerard
  • 2,832
  • 3
  • 27
  • 39
11

Try the show method in this way:

new media controller().show(50000);

And also check http://developer.android.com/reference/android/widget/MediaController.html#show().

SudeepSR: Please make a note of that, if you called show(0), it will show the Media Controller until hide() is called.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
6

What you need to do is, overrride the hide method in the custom controller and do nothing.

public class MyMediaController extends MediaController {
..
@Override
    public void hide() {
        // Do nothing here in order to always show

    }
...
}

PS: You still need to click on the video to show the media controller.

Gerard
  • 2,832
  • 3
  • 27
  • 39
Neo
  • 1,181
  • 11
  • 22
  • How to show media controller without selecting any video - ofcourse it won't be functional until video is selected - but to make it occupy that space. – Jasper Jan 07 '15 at 05:45
6

After trying all that I could, the following code worked for me!

    mVideoView = (VideoView) findViewById(R.id.video);

    mMediaController = new MediaController(this) {
        //for not hiding
        @Override
        public void hide() {}

        //for 'back' key action
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                Activity a = (Activity)getContext();
                a.finish();
            }
            return true;
        }
    };

    mMediaController.setAnchorView(mVideoView);
    mMediaController.setMediaPlayer(mVideoView);
    mVideoView.setMediaController(mMediaController);
    mMediaController.requestFocus();

    //only this showed the controller for me!!
    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mVideoView.start();
            mMediaController.show(900000000);
        }
    });

    //finish after playing
    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            finish();
        }
    });
RRK
  • 375
  • 3
  • 8
2

This may be an old thread, but still unanswered, try this :

final MediaController mediaController = new MediaController(this);
mediaController.setAlwaysDrawnWithCacheEnabled(true);
mediaController.setAnchorView(vView);
mediaController.requestFocus(); 
    vView.setOnPreparedListener( new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mediaController.show( 0 );
        }
    });

vView.setVideoPath(Preview_Path);
vView.setMediaController(mediaController);
vView.start(); 

theres a comment inside the MediaController Class "show" method

**Use 0 to show
* the controller until hide() is called**

so using 900000 or larger value wont help. hope it helps you.

cheers.

ralphgabb
  • 10,298
  • 3
  • 47
  • 56
2

Try this:

videoView.setOnCompletionListener(onVideoCompleted);
videoView.setOnPreparedListener(onVideoPrepared);

mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);

MediaController mc = new MediaController(this);
videoView.setMediaController(mc);

MediaPlayer.OnPreparedListener onVideoPrepared = new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mc.show(0);
    }
};

MediaPlayer.OnCompletionListener onVideoCompleted = new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        mc.hide();
    }
};
abhi
  • 503
  • 6
  • 24
0

I wanted to fade the controller for videos and always show it for audio. This worked

    mController = new MediaController(this) {
        @Override
        public void hide() {

            if (mType != TYPE_AUDIO) super.hide();
        }

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                mController.hide();
                Activity a = (Activity)getContext();
                a.finish();
                return true;
            }
            return false;
        }
    };

In MediaPlayer.onPrepared I added:

if (mType == TYPE_AUDIO) mController.show(0);

This causes the controller to show at the start of audio playback, but not video playback.

The other phone control buttons continue to work as normal.

Chris Read
  • 307
  • 4
  • 11
0

Easy! Set visibility "GONE" in event hide and set visibility "VISIBLE" in show!

        MediaController mc= new MediaController(zoom.this){

            @Override
            public void setMediaPlayer(MediaPlayerControl player) {
                super.setMediaPlayer(player);
                this.show(4000);
            }
            @Override
            public void show(int timeout) {
                super.show(timeout);
                this.setVisibility(View.VISIBLE);
            }
            //instead of press twice with press once "back" button to back
            @Override
            public boolean dispatchKeyEvent(KeyEvent event) {
                if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                    Activity a = (Activity)getContext();
                    a.finish();
                }
                return true;
            }
            @Override
            public void hide() {
                // TODO Auto-generated method stub
                super.hide();
                this.setVisibility(View.GONE);
                //super.show(3000);
            }

        };