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

时间:2015-05-20 05:09:02

标签: android android-videoview listeners

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

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char s[]= "64 0 5 @ 0 5";
        int length = strlen(s);
        int i = 0;
        char ch;
        while((ch = s[i]) != '\0')
        {
            int charASCII = ch;
            if(charASCII == 64)
                printf("Perform Algorithm\n");
            else
                printf("Do not Perform Algorithm\n");
            i++;
        }
        return 0;
    }

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(); } } 仅在ProgressDialogvideoView.setOnPreparedListener中被驳回。但是videoView.setOnErrorListener并没有被解雇,视频也没有被播放。我尝试放置日志并查看,日志打印到ProgressDialog之前,之后没有显示日志。我估计videoView.setOnPreparedListener没有注册。

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

编辑:

'M试图直播视频,如果视频是免费的,则应该转到Listeners并播放视频。如果Live不可用(即视频将在一段时间后生效),则应转到videoView.setOnPreparedListener并返回上一个videoView.setOnErrorListener,结果为“1”

视频通过Activity

进行流式传输

1 个答案:

答案 0 :(得分:3)

经过大量研究并在Preethi Rao的帮助下,我知道故障发生在URL

URL可能有视频(如果是实时视频的时间)并且可能没有视频(如果实时视频将在一段时间后流式传输)。如果我在Live Video不可用时尝试流式传输视频,则Listeners不会被解雇。如果Live Video是免费的,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();
    }
}