I am adding MediaController to a VideoView, but it does not show up unless I tap the phone. The controller disappears after a while.
Is there a way I can have the MediaController show always?
Thanks
Chris
As Default mediacontroller will hide in 3 seconds of user's inactivity. you can set the timeout seconds by
new media controller().show(50000);
check this link
The below code shows MediaController always:
VideoView videoView;
MediaController mc;
videoView.setMediaController(new MediaController(this)
{
public void hide()
{
System.out.println("HIDEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEHELLLOO");
mc.show();
}
});
Related
I'm trying to create a custom video playback, which has a video view and media controller:
VideoView mVideoView =(VideoView) findViewById(R.id.videoView);
mVideoView.setVideoURI(uri);
MediaController mMediaController = new MyMediaController(this);
mMediaController.findFocus();
mMediaController.setEnabled(true);
mMediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mMediaController);
Problem is that: when I programmatically change value of seekbar:
mVideoView.pause();
mVideoView.seekTo(newValue);
MediaController doesn't update these changes immediately. It updates only after I touch it again. Can anybody tell me the right way to update state of media controller? Thanks a a lot!
for this you have to set an handler that is call after every one second like below code-
static Runnable musicSchedular = new Runnable() {
#Override
public void run() {
resetSeek();
}
};
public static void resetSeek(){
long currentDuration = mediaPlayer.getCurrentPosition();
int cPos = (int)currentDuration/1000;
seekBar.setProgress(cPos);
}
and when you start playing music paste this line of code
myHandler.postDelayed(musicSchedular,100);
hope it will solve your problem.
Am I missing something? Once I play a video using videoview, I cannot see the current time of the video on the media controller. (Running app on a ICS device, there is no time; however on a Honeycomb device there is time)
Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.videoView);
// Use a media controller so that you can scroll the video contents
// and also to pause, start the video.
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView
.setVideoURI(Uri
.parse("rtsp://v6.cache5.c.youtube.com/CiILENy73wIaGQmCZld_oqDeJhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"));
videoView.start();
}
Actually I noticed the time is there but it's so dim and I cannot see it. Is there any way that I can change the theme of the media controller?
The answer is kind of general to all of the controllers such a mediacontroller, dialog, toast, etc.
you can use ContextThemeWrapper:
instead of:
MediaController mediaController = new MediaController(this);
you can use:
MediaController mediaController = new MediaController(new ContextThemeWrapper(this,R.style.CustomTheme));
can anyone explain this code of media controller?
videoview = (VideoView) findViewById(R.id.player);
videoview.setVideoURI(Uri.parse("android.resource://"+ getPackageName()+ "/" +R.raw.sample));
videoview.start();
MediaController controller = new MediaController(this);
controller.setMediaPlayer(videoview);
videoview.setMediaController(controller);
The code gets a VideoView by its id, sets the uri of the file to play to an embedded resource and starts playback.
Then it attaches a MediaController to the view which renders the progress bar and some controls to play / pause the video.
I have a video player in my android app. By default, the MediaController becomes visible upon playing for 3 seconds, if no activity.
The app goes through a series of short clips and that is fine, however, the MediaController pops up every time the video changes.
Is it possible to have it only show on demand (tapping the video view)?
I tried calling controller.hide() and controller.show(0) after I call vidPlayer.start(), but neither have worked. Any ideas?
final MediaController mc = new MediaController(this);
vView.setMediaController(new MediaController(this)
{
#Override
public void hide()
{
mc.show();
}
});
vView.setMediaController(mc);
vView.start();
I am using MediaController in my app. But unfortunately the MediaController disappears after a time period of 3 secs. But I want my MediaController to be visible until my video plays fully.
How to achieve this.
By default the MediaController hides itself after 3 secs of time. In order to make it visible throughout our video playback we will have to override the hide() of MediaController. I have given the code snippet below.
final MediaController mc = new MediaController(this);
video.setMediaController(new MediaController(this) {
#Override
public void hide()
{
mc.show();
}
});
video.setMediaController(mc);
For stop hiding the MediaController we can make a new Mediacontroller by extending the base class. Then we can disable the hide method by simply overriding it. For getting the actual hide functionality, we can fetch the hide() method in base class. We can hide the Mediacontroller after playback is completed using that.
Here is the code for MediaController:
public class MediaController_2 extends MediaController{
public MediaController_2(Context context) {
super(context);
}
public void hide() {
}
public void hidecontroller() {
super.hide();
}
}
Now the mediacontroller won't be hiding even after the completion of the playback. For hiding the controllers after completing playback we can use OnCompletionListener.
MediaController_2 mediaController = new MediaController_2(getActivity());
mediaPlayer.prepare();
mediaPlayer.start();
mediaController.show(0);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mediaController.hidecontroller();
}
});
More Succinct
First off, using an int timeout as a parameter in the show() method did nothing for me.
Secondly, this succinct piece of code forced the controls to stay on the screen after the first touch:
// Assumes you have a VideoView in your layout called 'video_preview'.
VideoView videoPreview = (VideoView) findViewById(R.id.video_preview);
MediaController mediaController = new MediaController(this) {
#Override
public void hide() {} // Prevent hiding of controls.
};
videoPreview.setMediaController(mediaController);
VideoView videoPlayer;
MediaController mediaController;
videoPlayer = view.findViewById(R.id.videoPlayer);
Uri uri = Uri.parse(URL);
videoPlayer.setVideoURI(uri);
mediaController = new MediaController(getContext()) {
#Override
public void hide() {} // on hide do nothing
};
videoPlayer.setMediaController(mediaController);
videoPlayer.requestFocus();
videoPlayer.start();