android - Mediaplayer prepare showing Illegal State Exception -


i trying play audio file internal storage. code used is..

package com.abhi.firstapp.firstapp; import android.content.context; import android.media.audiomanager; import android.media.mediaplayer; import android.net.uri; import android.os.bundle; import android.os.environment; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.util.log; import android.view.view; import android.view.menu; import android.view.menuitem; import android.widget.toast;  import java.io.file; import java.io.ioexception; import java.net.uri;  public class mainactivity extends appcompatactivity {       mediaplayer mp;      @override     protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          setcontentview(r.layout.activity_main);          toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);          setsupportactionbar(toolbar);        file f= new file("/sdcard/a.mp3");     if(f.exists())     {          toast toast= toast.maketext(this, "file exists", toast.length_long);         toast.show();         log.d("uri","1");         uri uri= uri.fromfile(f);         log.d("uri", "2");          mp= new mediaplayer();         mp.setaudiostreamtype(audiomanager.stream_music);         log.d("uri", "3");          try {             mp.setdatasource("/sdcard/a.mp3");         } catch (ioexception e) {             e.printstacktrace();         }         //mp.setdatasource(getbasecontext(), uri);             log.d("uri", "4");           try {             mp.prepare();         } catch (ioexception e) {             e.printstacktrace();             log.d("uri", "ioexception");         }          mp.start();       }     else {         toast toast1 = toast.maketext(this, "file not exist", toast.length_long);         toast1.show();     }      //mediaplayer mp= mediaplayer.create(getbasecontext(), uri);     //mp.start();  }    @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); } } 

by using log, can determine code running till mp.prepare(mediaplayer prepare). , on step, gives error illegal state exception

 caused by: java.lang.illegalstateexception    @ android.media.mediaplayer.prepare(native method) 

please help!

there couple of things might want change.

first: mp.prepare() block main thread, forbidden , result in exception android close app. prevent this, mp.prepareasync designed. use method instead , implement both onpreparedlistener , onerrorlistener.

second: should provide datasource before call prepare().

you example way:

public class mainactivity extends appcompatactivity implements mediaplayer.onpreparedlistener, mediaplayer.onerrorlistener {      protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          ...          mediaplayer mp = new mediaplayer();         mp.setaudiostreamtype(audiomanager.stream_music);          try {              mediaplayer.setdatasource(streamurl);         } catch (ioexception e) {             // error,         }          mp.prepareasync();         ...     }         @override     public void onprepared(mediaplayer player) {         mediaplayer.start();     }      ...  } 

Comments