With Media Player, there are a few questions, remember
Usage is not said, when using the best reference to the MediaPlayer of this picture
The first error is Media Player called in State 8
This is because there was no prepare before calling start, because I used the
MediaPlayer = mediaplayer.create (context, r.raw.notice);
To initialize the player, this interface illustrates
As long as a successful return to the player does not need to go to prepare, but I call stop and start many times later, resulting in the above error,
It can also be seen on the control chart that if you use stop when you pause, you must re-prepare before start or error Media Player called in State 8
If pause is paused, the direct start can be used without prepare.
The second error is Media Player called in State 0
This error is due to
public void Play () {try {mediaplayer.prepare ();} catch (illegalstateexception e) {e.printstacktrace ();}
Mediaplayer.start ();
}
Change it to the following.
public void Play () {try {mediaplayer.setonpreparedlistener (preparedlistener); Mediaplayer.prepareasync ();} catch ( illegalstateexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Onpreparedlistener Preparedlistener = new Onpreparedlistener () {@Overridepublic void onprepared (MediaPlayer MP) { Mediaplayer.start ();}};
A look to understand, just at the beginning I finished prepare went directly to start, but this time may prepare has not succeeded, so reported the above error.
Then you encounter a prepareasync called in State 8
This workaround adds a line of code to the previous one, ensuring that the player has been stop before prepare
public void Play () {try {if (MediaPlayer!=null) {mediaplayer.stop ();} Mediaplayer.setonpreparedlistener (Preparedlistener); Mediaplayer.prepareasync ();} catch (illegalstateexception e) {e.printstacktrace ();} Currentvolumn = Am.getstreamvolume (Audiomanager.stream_music), if (Currentvolumn < Maxvolumn) {Am.setStreamVolume ( Audiomanager.stream_music, Maxvolumn, 0);}}
jason0539
Weibo: http://weibo.com/2553717707
Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)
MediaPlayer use of Android error media Player called in State *,androidmediaplayer