Android - Media controller doesn't show up

Sotiris Falieris picture Sotiris Falieris · Feb 1, 2012 · Viewed 7.2k times · Source

I have a videoView where I show a video and I want to show the default media controllers. For some reason the controllers don't seem to want to show up.

I have tried creating the MediaController with xml, setting it to be always visible, attaching it to the media player with mMediaController.setMediaPlayer(mVideoView) but nothing seems to work.

I am using the classic video player code from Google which can be found here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html

What can be happening ? Does the listener lose the event ? Is it not attached to the actual video playing ? Should I add something else to the code that I am using (see below) ?

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_content_video);

    [...]

    mVideoView = (VideoView) findViewById(R.id.surface);
    mainVideoHolder = (LinearLayout) findViewById(R.id.main_video_holder);


    holder = mVideoView.getHolder();
    holder.addCallback(this);

    mMediaController = new MediaController(this);

    mMediaController.show();
}



private void playVideo() {
    doCleanUp();
    try {
        mMediaPlayer = new MediaPlayer();
        Log.d(tag, "surfaceCreated");
        File f = new File(mAssetsPath);
        File[] files = f.listFiles();
        Log.d(tag, "File: " + files[0].toString());

        URI uri = URI.create("file://" + (files[0].toString()));
        File file = new File(uri);
        ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
        mMediaPlayer.setDataSource(parcel.getFileDescriptor());

        mMediaPlayer.setDisplay(holder);

        mMediaPlayer.prepare();

        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);

        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        mMediaController.setMediaPlayer(mVideoView);

        mVideoView.setMediaController(mMediaController);
    } catch (Exception e) {
        Log.e(tag, "error: " + e.getMessage(), e);
    }
}


public void surfaceCreated(SurfaceHolder holder) {
    Log.d(tag, "surfaceCreated called");
    playVideo();
}

Any ideas would be greatly appreciated ?

There is no theme applied at the activity and the video plays normally without giving any error. It's just that the media controlos don't show up !

Thanks.

Answer

RRK picture RRK · Aug 21, 2014

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