Keeping MediaController on the screen in a VideoView

joachim picture joachim · Jul 11, 2011 · Viewed 14.9k times · Source

I have trouble finding a way to keep the MediaController on the screen when using it with a VideoView. I want to play an Audio file which is located in res/raw in my app. The file is playing but I would like to keep the controller on the screen for the user to see the length of the file and how much time is left before the end etc. I found many time on forums that we should use the method .show(time) in order to do that, but I cannot figure out how to make it work.

Here is the code I am using:

package com.sample.VideoViewExample;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewExample extends Activity implements SurfaceHolder.Callback{
   private VideoView mVideoView;
   private MediaController mMedia;

   @Override
   public void onCreate(Bundle icicle) {
     super.onCreate(icicle);
     setContentView(R.layout.main);

     mVideoView = (VideoView) findViewById(R.id.surface_view);
     //mVideoView.getHolder().addCallback(this);
     //mMedia.show(50000);
     //mVideoView.setMediaController(mMedia);

     MediaController mMedia = new MediaController(this);
     mMedia.setMediaPlayer(mVideoView);
     mMedia.setAnchorView(mVideoView);
     mVideoView.setMediaController(mMedia);

     mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.osa_patient)); 

     mVideoView.requestFocus();
     mVideoView.start();

   }

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mMedia.show(500000);

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}
}

When I add the line mVideoView.getHolder().addCallback(this); the app bug, I am not sure why.

Any help or alternative to what I would like to do is more than welcome :)

Thanks, JB

Answer

maxwells picture maxwells · Jan 2, 2012

this worked for me. Just extend the media controller class. And override the hide method.

    MediaController mediaController = new MyMediaController(this, true);

public class MyMediaController extends MediaController {

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

    @Override
    public void hide() {
        mediaController.show(0);
    }

}