Android (Java) Learning note 180:android MediaPlayer play Prepareasync called in the State 8 workaround

Source: Internet
Author: User

When playing an audio file using Android MediaPlayer, there are sometimes prepareasync called in the State 8 error.

The following methods can prevent this exception from appearing.

The first method:

Private voidPlayaudio (String audiouri) {URI Uri=Uri.parse (Audiouri); Mmediaplayer=mediaplayer.create (Getapplicationcontext (), URI); Try { //before playing, judge whether the Playermusic is occupied, so that the error will not be        if(Mmediaplayer! =NULL) {mmediaplayer.stop (); } mmediaplayer.prepare ();   Mmediaplayer.start (); } Catch(IllegalStateException e) {e.printstacktrace ();}Catch(IOException e) {e.printstacktrace ();}}

The second method:

Try { if(!isplaying) {isplaying=true; //Loading ResourcesMediaPlayer = Mediaplayer.create (playsdmp3activity. This, r.raw.trouble); Isreleased=false; Mediaplayer.setlooping (true);  Mediaplayer.start (); Mtextview.settext ("Music is currently playing!" "); }} Catch(IllegalStateException e) {//TODO auto-generated Catch blocke.printstacktrace ();}

Mediaplayer.create (CONTEXT,RESID) This way to configure the data source, it completes the initialization, so do not prepare can directly start, we must always remember Nullpoint Exception, so here to add judgment:

 !isplaying   and     mmediaplayer! = null

Android (Java) Learning note 180:android MediaPlayer play Prepareasync called in the State 8 workaround

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.