Search code examples
androidvideo-streamingmedia-playerandroid-videoviewhttp-live-streaming

Audio Playing But Video not Showing in VideoView Android (Black Screen in Low Internet)


I am Working on Video Streaming in Android. when I want to play video in Low Internet Speed video is not appear in the VideoView show black Screen but Audio is playing fine. i have see these links link1 and link2 . But these link couldn't help me. Here is my code snapshot:-

private void playvideo(String url){
    final MediaController mediacontroller;
    try {
        mediacontroller = new MediaController(MainActivity.this);
        mediacontroller.setAnchorView(videoView);
        final Uri video = Uri.parse(url);
        videoView.setMediaController(mediacontroller);
        videoView.setVideoURI(video);
        videoView.requestFocus();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                mp.start();
                mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
                        if (extra == MediaPlayer.MEDIA_ERROR_SERVER_DIED
                                || extra == MediaPlayer.MEDIA_ERROR_MALFORMED) {
                            Log.e(TAG , "MEDIA_ERROR_SERVER_DIED && MEDIA_ERROR_MALFORMED");
                            Toast.makeText(MainActivity.this , "Server Died or Malformed" , Toast.LENGTH_LONG).show();
                            return true;
                        } else if (extra == MediaPlayer.MEDIA_ERROR_IO) {
                            Log.e(TAG , "MediaPlayer.MEDIA_ERROR_IO");
                            Toast.makeText(MainActivity.this , "Media Error IO" , Toast.LENGTH_LONG).show();
                            return true;
                        }else if (extra == MediaPlayer.MEDIA_ERROR_UNKNOWN){
                            Log.e(TAG , ".MEDIA_ERROR_UNKNOWN");
                            Toast.makeText(MainActivity.this , "MEDIA ERROR UNKNOWN" , Toast.LENGTH_LONG).show();
                            return true;
                        }
                        return false;
                    }
                });
                mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                    @Override
                    public void onBufferingUpdate(MediaPlayer mediaPlayer, int percentage) {
                        Log.e(TAG , "onBufferingUpdate :- " + percentage);
                    }
                });
                progressBar.setVisibility(View.GONE);
                videoView.start();
            }
        });
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        progressBar.setVisibility(View.GONE);
        Toast.makeText(getApplicationContext() ,"URL is Wrong Test with other URL" , Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

because Audio and Video both are different Streams so in Low Internet somehow it able to download the audio stream but not a video stream, is it ?? so in this case, it is possible to show a message to the user like "you have Low Internet Try Again" and not to play Audio ??

thanks


Solution

  • Try to use Exoplayer which is an alternative to Android’s MediaPlayer API for playing audio and video both locally and over the Internet.

    https://github.com/google/ExoPlayer