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
In Android, VideoView is used to display a video file. It can load images from various sources (such as content providers or resources) taking care of computing its measurement from the video so that it can be used for any layout manager, providing display options such as scaling and tinting.
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);
}
}
Ok I have found! After spending hours on it!! So there is to my knowledge no clue on how to do it on the different forums. Here is a trick I found:
FIRST CLASS (example...)
package com.sample.VideoViewExample;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.Window;
import android.view.WindowManager;
import android.widget.VideoView;
public class VideoViewExample extends Activity implements SurfaceHolder.Callback{
private VideoView mVideoView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVideoView.setKeepScreenOn(true);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
SurfaceHolder holder = mVideoView.getHolder();
holder.addCallback(this);
MediaController_2 mMedia = new MediaController_2(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) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
SECOND CLASS (WITH THE TRICK)
package com.sample.VideoViewExample;
import android.content.Context;
import android.widget.MediaController;
public class MediaController_2 extends MediaController{
public MediaController_2(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void hide() {
}
}
the trick is just to create a new MediaController that extends Mediacontroller and which function hide() does nothing!
If you want to have a look at the MediaController source code for a better understanding, you can look here: // source code for MediaController http://hi-android.info/src/android/widget/MediaController.java.html
Hope this will be of help to sb, JB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With