MediaController always show on Android
Asked Answered
A

10

25

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.

Andantino answered 16/12, 2010 at 7:54 Comment(0)
S
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);
Sacramental answered 7/12, 2011 at 2:57 Comment(0)
V
13

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.
    }
};
Vilmavim answered 24/10, 2013 at 19:37 Comment(1)
Why the downvote? It's common courtesy to explain when downvotingVilmavim
R
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;
        }
    };
Ravi answered 19/1, 2014 at 11:21 Comment(4)
My volume controls (on screen and device buttons) don't work at all when I override these functions.Marinna
Code in last post at (#23339489) works perfectly.Marinna
Only return true if event.getKeyCode() == KeyEvent.KEYCODE_BACK. Otherwise return false and then the other device buttons will continue to workOlimpiaolin
Also, add mediaController.hide() before finishing the activityOlimpiaolin
P
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.

Pournaras answered 16/12, 2010 at 8:20 Comment(0)
A
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.

Aardvark answered 30/7, 2012 at 5:8 Comment(1)
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.Mcclure
M
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();
        }
    });
Marillin answered 21/8, 2014 at 19:28 Comment(0)
B
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.

Burr answered 24/2, 2015 at 1:41 Comment(0)
R
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();
    }
};
Rusell answered 20/8, 2015 at 2:27 Comment(0)
O
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.

Olimpiaolin answered 7/3, 2017 at 14:31 Comment(0)
U
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);
            }

        };
Ulrica answered 14/7, 2017 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.