MediaController always show on Android

懵懂的女人 提交于 2019-12-03 04:39:02

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);

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;
        }
    };
Sankar Ganesh

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.

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.
    }
};
Neo

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.

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();
        }
    });

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();
    }
};

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.

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.

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);
            }

        };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!