Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept the “Cannot play video” dialog while working with streaming or networked video?

How to intercept the "Cannot play video" dialog while working with streaming or networked video?

I tried the following and was able to display my custom error message. But on top of that I am still getting Android MediaPlayer error dialog "Cannot play video".

I have implemented setOnErrorListener for MediaPlayer and overwrote the onError method with logic to show my error message and I am returning true to let Android know I am handling this error. My code is as follows

mediaPlayer.setOnErrorListener(new OnErrorListener() {
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        if (!mediaActivity.isFinishing()) {
            mVideoView.stopPlayback();
            mediaPlayer.release();
            showErrorDialog(false);
        }
        return true;
    }
});
like image 775
AndyW Avatar asked Dec 16 '11 19:12

AndyW


1 Answers

Here is the my working code!

    videoView = (VideoView) findViewById(R.id.videoViewPopup);
    MediaController mc = new MediaController(this);
    mc.setAnchorView(videoView);
    mc.setMediaPlayer(videoView);
    Uri video = Uri.parse(this.mediaURL); // Put your URL here
    videoView.setMediaController(mc);

    videoView.setOnErrorListener(new OnErrorListener() {

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Utils.showAlertDialog(Constants.NETWORK_ERROR_MESSAGE, Constants.NETWORK_ERROR_TITLE, VideoPopupActivity.this);
            return true;
        }
    });

    videoView.setVideoURI(video);
    videoView.start();      
like image 133
James Avatar answered Sep 28 '22 06:09

James