I have an Activity that takes a url and plays the video with a VideoView. This works fine.
However, there is a buffer time before the video starts playing. Because of this, the VideoView is black. There is no spinner indicating a buffer loading.
Is there a parameter of the VideoView that will show a spinner? If not, is there some message that gets broadcast when the video starts playing? That way I can show my own spinner and hide it when the message is received.
I've found the answer. Add a ProgressBar over the top of the VideoView and do the following:
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mSpinner.setVisibility(View.GONE);
}
});
Related
I'm using VideoView to loop a small video, all works fine on the emulators, but when I deploy that to TV, after the first loop video turns black, but sound keeps going. This is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
VideoView videoview = (VideoView) findViewById(R.id.videoview1);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video);
videoview.setVideoURI(uri);
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
videoview.start();
}
});
}
The Emulator is using Android 6.0, the TV is Sony Bravia with Android 6.0.1.
I tested using SurfaceView, instead of VideoView - the same thing happens.
Any idea how to get rid of that black screen?
PS: There is a workaround that works - make OnCompletionListener and do videoview.start() there - this way it loops, but there's an ugly gap between the loops.
Just use this mVideoView.setZOrderOnTop(true); It will not show the black screen as the view appears.
Try to set your VideoView using handler like this.
videoview.setBackgroundColor(Color.WHITE); //color what you want as background
videoview.postDelayed(new Runnable() {
#Override
public void run() {
videoview.setVideoURI(videoUri);
}
}, 100);
videoview.postDelayed(new Runnable() {
#Override
public void run() {
vv.setBackgroundColor(Color.TRANSPARENT);
}
}, 300);
videoview.requestFocus();
videoview.start();
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { #Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
I have looked into the source code of VideoView and the first thing that I would like to point out is that inside the setVideoURI() method the MediaPlayer object will be created, and the OnPreparedListener will be set.
Your mistake is that you set the OnPreparedListener after the MediaPlayer may already have prepared the video, thus never call the onPrepared in the listener you have set after that, which means the setLooping(true) was maybe never set on the MediaPlayer.
TL;DR:
Put videoview.setVideoURI(uri); after videoview.setOnPreparedListener().
videoview.start() should be after setVideoURI(). On finish it should start again without additional input because this time mp.setLooping(true); was correctly set and will be executed. No videoview.start() is necessary after the initial one.
I actually ended up using ExoPlayer instead of the default one.
It's a bit harder to set up, but this problem didn't show up there.
Hi in my android application I am using videoview for streaming my videos. I face very unwanted behaviour from my videoview. The scenario is like this. I have activity A and activity B. Activity A has one simple button and on click of that button I am starting activity B which contains videoview and starts playing video as soon as it start my activity. So my observation is like this: Once I start Activity B it will call setVideoURI and start(). there are few callback methods one of them is setOnPreparedListener. when I call start() after some time It is executing setOnPreparedListener and after that it will start playing video. But in between before executing setOnPreparedListener if I come back to Activity A it will block that activity UI for some time. But if I wait till setOnPreparedListener get executes and then come back to Activity A then its working properly. This is not happening with all devices only with google devices like moto g and nexus. But I tried with htc or intel device it is working properly. My code looks like :
VideoView mVideoView =(VideoView)findViewById(R.id.myVideo);
//Creating MediaController
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(mVideoView);
//specify the location of media file
Uri uri=Uri.parse("http://abcExample.com/playlist.m3u8");
//Setting MediaController and URI, then starting the videoView
mVideoView.setMediaController(mediaController);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
mVideoView.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.i("this is video view sample ... ", "this is video view sample ... this is on error listener ");
return true;
}
});
mVideoView.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
Log.i("this is video view sample ... ", "this is video view sample ... this is on prepared listener ");
}
});
mVideoView.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
Log.i("this is video view sample ... ", "this is video view sample ... this is on complete listener ");
}
});
Am I missing something or doing something wrong? Need some help. Thank you.
I'm having the same behaviour with videoview. I was looking for a solution but i didn't find anything. Maybe, It's possible that making a new task fix that issue. I'll try it
I want to play video in a VideoView. which one is correct?
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
video.start();
}
});
or simply:
video.start();
Both method is right to play video but setOnPreparedListener is useful when you want to show ProgressBar when media is loading for play from SDCARD,webserver or streaming urls.
and if video.start(); called without setOnPreparedListener some delay occur during loading file then only black screen appear to user until video start.
I have used MediaController in my activity its working fine but when I play video for first time then there should b pause button visible but instead there is play and when I press that button then the video is paused correctly and state remains the same and after that its working properly. And same thing happens when Video Completed.
Is this a bug or I am doing any thing wrong?
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaController = new MediaController(VideoPlayerActivity.this){
public void hide(){
}
public void show(){
if(isPlayingAd){
super.hide();
}else{
super.show();
}
}
};
videoView.setMediaController(mediaController);
mediaController.setMediaPlayer(videoView);
mediaController.show();
}
});
I've been having the same issue. I was not calling MediaController.setVideoView as you were, as I thought VideoView.setMediaController was sufficient for wiring things up. I tried adding that, then moving the call to show within onPrepared, and now it is working.
I wish I had a better understanding; my best guess is that perhaps everything needs to be wired up properly before the media is prepared, and before calling show. In any case, here is what I have:
mMediaController = new MediaController(VideoPlayerActivity.this, false);
mVideoView.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer pMp) {
mMediaController.show();
}
});
mVideoView.setMediaController(mMediaController);
mMediaController.setMediaPlayer(mVideoView);
mVideoView.setVideoPath(uri); // may not be applicable in your case
mVideoView.requestFocus();
mVideoView.start();
As Oneworld mentioned on the other answer, I had same issue with old Samsung devices. Even though MediaController wired with VideoView properly, play button loses its sync until pause and play again with MediaController.
This thing seems only happens on old Samsung devices(KitKat and below i guess).
The only solution I found was play video programmically by videoview.start() before showing controller by mc.show().
I am using VideoView to play video.
If the video is not supported by the phone (such as my phone supporting only 480p video, not 720p which is the video I'm trying to play), it shows the dialog
Sorry,This video cannot be played
I want to do something after I click the dialog's "OK" button.
Where can I add the code to do this?
My code as below:
vv = (VideoView)this.findViewById(R.id.screen_video);
Uri uri = Uri.parse(FlePath);
vv.setVideoURI(uri);
vv.start();
After your code paste this.If you get this error then oncmpletion listner will call.
vv.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//release your MediaPlayer Resource
//do whatever you want
}
});
Hope this help you :)