Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

videoView.setOnPreparedListener, videoView.setOnCompletionListener and videoView.setOnErrorListener not getting called

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

like image 904
Logic Avatar asked May 20 '15 05:05

Logic


1 Answers

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();
    }
}
like image 125
Logic Avatar answered Sep 19 '22 11:09

Logic