简体   繁体   中英

Android VideoView in dialog black screen on reload

I have a VideoView inside a custom Dialog. 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. 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. 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.

Note:: You need to use both onCreateDialog(int id) as well as onPrepareDialog(int id).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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