简体   繁体   English

对话框黑屏上的Android VideoView重新加载时

[英]Android VideoView in dialog black screen on reload

I have a VideoView inside a custom Dialog. 我在自定义对话框中有一个VideoView。 The first time the Dialog is shown, the video is played correctly, but if the Dialog is dismissed and then loaded again, the video doesn't play and the dialog is just a blackscreen. 第一次显示该对话框时,视频可以正确播放,但是如果关闭该对话框然后再次加载,则该视频将无法播放,并且该对话框只是一个黑屏。

Here is the code I use to create my Dialog : 这是我用来创建对话框的代码:

Dialog d;

protected Dialog onCreateDialog(int id) {
    switch(id) {
    case DIALOG_VIDEO:
        AlertDialog.Builder builder;

        LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.video,null);

        final VideoView vv = (VideoView)layout.findViewById(R.id.vv);
        vv.setMediaController(new MediaController(this));

        vv.setVideoURI(path);

        vv.setZOrderOnTop(true);
        vv.requestFocus();
        vv.start();

        builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        d = builder.create();
        d.setOnDismissListener(new DialogInterface.OnDismissListener() {
            public void onDismiss(DialogInterface dialog) {
                VideoView vv = (VideoView)d.findViewById(R.id.vv);

                vv.stopPlayback();
                vv.clearFocus();
            }
        });
        break;
    default:
        d = null;
    }

    return d;
}

What am I doing wrong ? 我究竟做错了什么 ?

Thank you for your time and sorry for my bad english. 谢谢您的时间,对不起我的英语不好。

The method onCreateDialog(int id) is called only once when your dialog is created. 创建对话框时,仅调用一次onCreateDialog(int id)方法。 So once you dialog is created and when you dismiss the dialog, now the method onCreateDialog(int id) is called not called again thats why you are unable to view the video for second time. 因此,一旦创建对话框并关闭对话框,就不会再调用onCreateDialog(int id)方法,这就是为什么您无法第二次观看视频。 As per your code ,the logic for playing for video is only for on creating the dialog. 根据您的代码,播放视频的逻辑仅用于创建对话框。

You can use onPrepareDialog(int id) which will be called every time you launches the dialog. 您可以使用onPrepareDialog(int id),每次启动对话框时都会调用它。

Note:: You need to use both onCreateDialog(int id) as well as onPrepareDialog(int id). 注意::您需要同时使用onCreateDialog(int id)和onPrepareDialog(int id)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM