Here is my code snippet where I want to play a video coming from server
private void PlayVideo() {
try {
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(VideoActivity.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videoPath);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoPlayer.removeAllViews();
videoPlayer.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);
videoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
dismissProgressDialog();
videoView.bringToFront();
videoView.setFocusable(true);
videoView.start();
contentStarted = true;
}
});
videoView.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
contentStarted = false;
}
});
videoView.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
dismissProgressDialog();
Intent in = new Intent();
setResult(1, in);
finish();
return false;
}
});
} catch (Exception e) {
dismissProgressDialog();
finish();
}
}
The ProgressDialog
is dismissed only in videoView.setOnPreparedListener
and videoView.setOnErrorListener
. But the ProgressDialog
is not getting dismissed and video is not getting played. I tried to put Logs and see, Logs are printed upto just before videoView.setOnPreparedListener
and after that no Logs are displayed. Listeners
are not getting registered I guess.
Any help is appreciated. Thanks in advance.
EDIT:
'M trying to stream a live video, if video is availbale it should go to videoView.setOnPreparedListener
and should play the video. If Live is not available(i.e, Video will be live after some time) then it should go to videoView.setOnErrorListener
and return to previous Activity
with result "1"
and
Video is streamed over RTSP
After a lot of research and with the help of Preethi Rao I got to know that the fault was in the URL
.
The URL
might have the video (if it is time for Live Video) and might not have video (if Live Video will be streaming after some time). If I'm trying to stream video when Live Video is not available then the Listeners
are not getting fired. If Live Video is availbale, Listeners
are getting fired. So, videoView.setOnPreparedListener
and videoView.setOnErrorListener
are not getting attached.
I wrote a handler to run for 60 seconds and if no Listeners
are attached I'm just returning to previous Activity
.
Here's the code:
private void PlayVideo() {
try {
isListenerAttached = false;
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(VideoActivity.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videoPath);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoPlayer.removeAllViews();
videoPlayer.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);
// Using this Handler to revert to previous Activity when the Video View is not attached to Listeners
// As the Buffering Video dialog doesn't get dismissed if Video View is not attached to Listeners
runOnUiThread(new Runnable() {
@Override
public void run() {
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
if(!isListenerAttached){
dismissProgressDialog();
Intent in = new Intent();
setResult(1, in);
finish();
}
}
}, WAIT_TIME);
}
});
videoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
isListenerAttached = true;
dismissProgressDialog();
videoView.bringToFront();
videoView.setFocusable(true);
videoView.start();
contentStarted = true;
}
});
videoView.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
contentStarted = false;
}
});
videoView.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
isListenerAttached = true;
dismissProgressDialog();
Intent in = new Intent();
setResult(1, in);
finish();
return false;
}
});
} catch (Exception e) {
dismissProgressDialog();
finish();
}
}
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