Android error MediaPlayer usage of media Player called in State *,androidmediaplayer

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.