Reputation: 9986
I'm using the Android's MediaPlayer
class to play some remote resources. I would like the user to be able to reuse the MediaPlayer
to open some content and then change it to play another one without having to recreate the MediaPlayer
.
So, I wrote a method to open a resource which, first of all, resets the MediaPlayer
so that I can send it to the idle state. After that, I set the new URI and I call the prepare method. It happens quite often, anyway, that the method setDataSource
hangs, for many seconds and even for minutes. This is the code:
mediaPlayer.reset();
mediaPlayer.setDataSource(this, Uri.parse(uri));
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.prepare();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Any idea why the method should hang for many seconds after setDataSource and before the setDisplay method? Thanks!
Upvotes: 3
Views: 4883
Reputation: 5040
This could be it: "Must call [setAudioStream] method before prepare() or prepareAsync() in order for the target stream type to become effective thereafter."
http://developer.android.com/reference/android/media/MediaPlayer.html#setAudioStreamType(int)
Upvotes: 1