Android VideoView ANR loading content from network

14 Views Asked by At

i am trying to load an HTTPS mp4 video URL from the Internet into a VideoView. General behaviour is good, but sometimes the users do not want to wait for the video to finish loading, and want to go back. I am always getting lots of ANR in my Google Play Developer Console. Here is the code of my implementation:

binding.videoView.setVisibility(View.VISIBLE);
binding.videoView.setOnPreparedListener(mp -> {
    //Here we check if user is still awaiting for video to play before calling start.
    mp.setLooping(true);
    mp.start();
    mp.setVolume(1, 1);
});
String url = "https://example.org/example.mp4"
binding.videoView.setVideoURI(Uri.parse(url));

@Override
public void onBackPressed() {
    binding.videoView.stopPlayback();
    binding.videoView.setVisibility(View.GONE);
    //other stuff..
}

And here is the ANR stack trace

  at android.media.MediaHTTPConnection.disconnect (MediaHTTPConnection.java:172)
  at android.media.IMediaHTTPConnection$Stub.onTransact (IMediaHTTPConnection.java:134)
  at android.os.Binder.execTransactInternal (Binder.java:1184)
  at android.os.Binder.execTransact (Binder.java:1143)
  at android.media.MediaPlayer._release (Native method)
  at android.media.MediaPlayer.release (MediaPlayer.java:2211)
  at android.widget.VideoView.stopPlayback (VideoView.java:365)
  at com.example.activity.MainActivity.goBack (MainActivity.java:3093)
  at com.example.activity.MainActivity.onBackPressed (MainActivity.java:2093)
0

There are 0 best solutions below