Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaController always show on Android

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.

like image 395
Bytecode Avatar asked Dec 16 '10 07:12

Bytecode


3 Answers

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);
like image 151
pcting Avatar answered Oct 20 '22 07:10

pcting


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;
        }
    };
like image 13
mkhan Avatar answered Oct 20 '22 06:10

mkhan


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.
    }
};
like image 11
Gerard Avatar answered Oct 20 '22 05:10

Gerard