Reusing Android MediaPlayer

I am using the Android class MediaPlayerto play some remote resources. I would like the user to be able to reuse MediaPlayerto open some content, and then change it to play another, without having to recreate MediaPlayer.

So, I wrote a method for opening a resource, which first of all resets MediaPlayer, so that I can send it to the standby state. After that, I set a new URI, and I call the preparation method. This happens quite often, one way or another, that the method setDataSourcefreezes for many seconds and even 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? Thank!

+3
source share
1 answer

It could be like this: "Must call the [setAudioStream] method before preparing () or prepareAsync () so that the type of the target stream becomes effective after that."

http://developer.android.com/reference/android/media/MediaPlayer.html#setAudioStreamType(int )

+1
source

Source: https://habr.com/ru/post/1760972/


All Articles