Android MediaPlayer IllegalStateException -
i trying play audio file in eclipse using mediaplayer in android programming. need run audio file (.wav or .mp3) saved on computer hard-drive. have tried many things. tried copying file in res folder of project, nothing worked. below code. can see have tried many ways , commented some. first getting ioexception earlier. after using mediaplayer.create(), getting illegalstateexception. appreciated.
public void playsound(mainactivity mainactivity){ system.out.println("in playsound"); mediaplayer mp = null; fileinputstream fis = null; try { //fis = new fileinputstream("c:\\users\\anupam-pc\\downloads\\trailsoundfilerecordings\\jagmeet.wav"); //mp.setdatasource(fis.getfd()); system.out.println("inside try"); //mp.setdatasource("/res/raw/hai.wav"); //mp.setdatasource("c:\\users\\anupam-pc\\downloads\\trailsoundfilerecordings\\jagmeet.wav"); //mp.setdatasource("android.resource://com.example.texttospeech/agreji.mp3"); //mp.setdatasource("agreji.mp3"); mp = mediaplayer.create(mainactivity, r.raw.hai); system.out.println("after ds"); mp.prepare(); system.out.println("after prepare"); mp.start(); // fis.close(); } catch (exception e) { system.out.println(e); } finally{ try{ fis.close(); } catch (exception e){ } }
you don't need implement mp.prepare();
, have create(/**/)
this example works charm:
class vulcan implements mediaplayer.onpreparedlistener{ mediaplayer mediaplayer = null; ... boolean isprepearedtoplayaudio = false; mediaplayer=mediaplayer.create(context,r.raw.mini_vulcan_gun_1); mediaplayer.setonpreparedlistener(this); public void stop(){ mediaplayer.pause(); } public void start(){ if(isprepearedtoplayaudio){ mediaplayer.start(); mediaplayer.setlooping( true ); } } @override public void onprepared(mediaplayer mp) { isprepearedtoplayaudio = true; }
Comments
Post a Comment