Android - MediaPlayer on Prepare Called before a stream is prepared on Android 4.0+

I ran into a problem that whenever a stream is played by my application in Android 4.0+, the OnPrepare method from MediaPlayer.OnPreparedListener is called before the stream loads and therefore I can not tell the user that the load / buffer stream is running. I already found a question of the same type, but did not answer. Here's what I'm doing.

  @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); playVideo(someRtspUrl); } private void playVideo(String url) { // if app is running on Google TV then change RTSP link to HLS if (url.contains("rtsp")) { // split the RTSP URL and make it as HLS String videoUrlParts[] = url.split("\\?"); url = videoUrlParts[0].replace("rtsp", "http") + "/playlist.m3u8"; if (videoUrlParts.length > 1) url += "?" + videoUrlParts[1]; } mVideoView.setVideoURI(Uri.parse(url)); mVideoView.requestFocus(); mVideoView.setOnCompletionListener(this); mVideoView.setOnPreparedListener(this); } @Override public void onPrepared(MediaPlayer player) { dismissProgressDialog(); mVideoView.start(); } 

This code works great on Google TV and other Android 3.0+ and <4.0+

+7
java android video-streaming media-player android-4.2-jelly-bean
source share
3 answers

I do not have much experience working with a media player. But a couple of suggestions / requests from my side

  • No need to cook. If you do this, have you tried prepareAsync?
  • You are not using a mediaplayer instance that is passed to the onPrepared callback. You might be trying to start the wrong media player.
+1
source share

I would suggest using the MediaPlayer class, which is available. Unlike VideoView, MediaPlayer knows about its state and manages even more for you what VideoView does not offer. It involves setting up a SurfaceHolder to display content in it quite simply.

You just want to make sure that you handle the state correctly and use the prepareAsync () call of MediaPlayer.

+1
source share

This is a known issue for .m3u8 with Android <4.3.

They recorded this in 4.3. It is worth comparing MediaPlayer or VideoView code on different platforms ie 4.3 and <4.3

+1
source share

All Articles