Search code examples
androidandroid-videoviewrtsp

when video not start launch a dialog to back main activity


this is my code to videoview

try {

            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    SingleItemView.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(Uri.parse(video));
            videoview.requestFocus();
            videoview.setKeepScreenOn(true);

            videoview
                    .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        // Close the progress bar and play the video
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mProgressDialog.dismiss();
                            videoview.start();
                        }
                    });
        } catch (Exception e) {
            mProgressDialog.dismiss();
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

but the dialog is still remain if video stream is not working..

i want to put dialog to back to main activity if video is not working..

how can i do that?

i try put dialog but still not working i dont know why..

thanks in advance :)

EDIT : i also try like this but still not working

try {
                    // Start the MediaController
                    MediaController mediacontroller = new MediaController(
                            SingleTrackActivity.this);
                    mediacontroller.setAnchorView(videoview);
                    // Get the URL from String VideoURL
                    videoview.setMediaController(mediacontroller);
                    videoview.setVideoURI(Uri.parse(duration));
                    videoview.requestFocus();
                    videoview.setKeepScreenOn(true);

                    videoview
                            .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                // Close the progress bar and play the video
                                public void onPrepared(
                                        MediaPlayer mediaPlayer) {
                                    pDialog.dismiss();
                                    videoview.start();
                                }
                            });
                } catch (Exception e) {
                    AlertDialog alertDialog = new AlertDialog.Builder(
                            SingleTrackActivity.this).create();

                    // Setting Dialog Title
                    alertDialog.setTitle("Alert Dialog");

                    // Setting Dialog Message
                    alertDialog.setMessage("Video Play Error :"
                            + e.toString());

                    pDialog.dismiss();

                    System.out.println("Video Play Error :" + e.toString());
                    finish();
                }

Solution

  • The media player work is done asynchronously from your code. If the media failed to setup, then the fail will occur after the code finished your method. At the same time, onPreparedListener wasn't called since the media did't enter in a PREPARED state. So try to add an OnErrorListener:

    videoview.setOnErrorListener(new MediaPlayer.OnErrorListener(){
        public boolean onError(MediaPlayer mp, int what, int extra) {
            // inform the user something went wrong
            mProgressDialog.dismiss();
            return true;
        }
    });
    

    If this doesn't work check what callback you're getting for your media player / videoview.