Reputation: 940
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.
Upvotes: 3
Views: 7930
Reputation: 375
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();
}
});
Upvotes: 1
Reputation: 41
this work for me:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
getWindow().setFormat(PixelFormat.UNKNOWN);
mVideoView = (VideoView)findViewById(R.id.videoview);
mc = new MediaController(this){
@Override
public void hide() {} //dont hide control
};
new MyDelay2().execute("");
}
private class MyDelay2 extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
Uri uri2 = Uri.parse("android.resource://com.blah/"+R.raw.cool);
mVideoView.setVideoURI(uri2);
return null;
}
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
mc.setAnchorView(mVideoView);
mc.setMediaPlayer(mVideoView);
mc.setEnabled(true);
mVideoView.setMediaController(mc);
mc.show();
}
}
Upvotes: 0
Reputation: 119
I'm not sure this answer is a little late but maybe someone else has that problem. The doku says the fellowing:
The way to use this class is to instantiate it programatically. The MediaController will create a default set of controls and put them in a window floating above your application. Specifically, the controls will float above the view specified with setAnchorView(). The window will disappear if left idle for three seconds and reappear when the user touches the anchor view.
So you can do it like this (the MediaPlayer is a VideoView in this case:
VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVideoPath(uri);
MediaController controller = new MediaController(this);
controller.setAnchorView(videoView);
controller.setMediaPlayer(videoView);
videoView.setMediaController(controller);
videoView.start();
This worked for me.
Upvotes: 0
Reputation: 38168
Where do you add your controller to a layout ? Also be sure the container is large enough. Give it a background and look if it displays well on screen before adding the video controller.
Upvotes: 0