Is resetting MediaPlayer safe in ready state?

I am writing an audio player using MediaPlayer, which allows the user to skip the real melody. A skip request may appear at any time, including between a call to MediaPlayer.prepareAsync and an upcoming call to onPrepared. Doc says:

It is important to note that the preparation state is a transition state, and the behavior of calling any method with a side effect when the MediaPlayer object is in preparation state is undefined.

Does this include calls to reset or even issue? Because if this is the case, then I will either have to wait for the onPrepared callback to reuse MediaPlayer or allocate a new MediaPlayer if I do not want to wait and release the obsolete from onprepared callback, right?

+8
android
source share
3 answers

In my opinion, I will follow the tips in the documents, I found several problems with the player on different devices (in some devices, reusing the same player is generally unstable).

I think a good option is to distribute the players and switch between them when the user skips the melody, and then you expect the original player to reach the prepared state, and then you reset safely.

+3
source share

I also created a streaming music player and struggled with the preparatory state. Worst of all, there were some threads where prepare () hung eternally loading (buffering) data without calling onBufferUpdate. The release call did nothing. So, as I did, I called reset () on the stuck MediaPlayer from anotherthread after 15 seconds, despite the recommendations in the docs. This made him throw an exception and lead him to an error state. After detecting the exception, I called release (). This seems to have solved the problem. Hope this helps someone.

+5
source share

I ran into a problem when the MP freezes when the preparation state (thread) is too large, and I try to stop it with reset (). This causes the MP to hang, and thus, my application freezes. There seems to be no way to stop MP in preparing state. Im thinking about using prepare () wrapped in a stream instead of prepareAsync (). Then I can kill this stream. At the moment, I have done it as follows:

try { mp.setDataSource(new String()); } catch (Exception e) { e.printStackTrace(); android.util.Log.d(TAG,"actionCancel(): mp.setDataSource() exception"); mp.reset(); } 

and it works 4me.

-one
source share

All Articles