views:

145

answers:

1

Hi! 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!

A: 

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)

greg7gkb
Seems the problem was related to the specific device I was using. I tested the same exact code under Android 2.2 (I was experiencing this issue under Android 2.1) on a different device and it's working like a charm.
Luca