videoView.setOnPreparedListener、videoView.setOnCompletionListener 和 videoView.setOnErrorListener 未被调用

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

这是我的代码片段,我想在其中播放来自服务器的视频

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();
    }
}

ProgressDialog 仅在 videoView.setOnPreparedListenervideoView.setOnErrorListener 中被忽略。但是 ProgressDialog 没有被解雇,视频也没有播放。我尝试放置日志并查看,日志打印到 videoView.setOnPreparedListener 之前,之后不显示任何日志。我猜 Listeners 没有注册。

感谢任何帮助。 提前致谢。

编辑:

'我正在尝试流式传输实时视频,如果视频可用,它应该转到 videoView.setOnPreparedListener 并播放视频。如果直播不可用(即,视频将在一段时间后直播),那么它应该转到 videoView.setOnErrorListener 和 return 到前一个 Activity,结果为“1”

视频通过 RTSP

流式传输

经过大量研究并在 Preethi Rao 的帮助下,我了解到故障出在 URL

URL 可能有视频(如果是实时视频的时间),也可能没有视频(如果实时视频将在一段时间后流式传输)。如果我在实时视频不可用时尝试流式传输视频,则 Listeners 不会被触发。如果实时视频可用,Listeners 将被解雇。所以,videoView.setOnPreparedListenervideoView.setOnErrorListener 没有得到附加。

我为 运行 写了一个处理程序 60 秒,如果没有附加 Listeners 我只是返回到之前的 Activity

代码如下:

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();
    }
}