java - MediaPlayer playing and replaying issue -


i trying set media player sound can played , replayed many times desired. getting error while playing audio first time:

08-23 14:48:52.613: e/mediaplayer(24194): error (1, -2147483648) 08-23 14:48:52.613: v/preschool basics(24194): prepare failed.: status=0x1 

my code calls play is:

    btnplay = (button) findviewbyid(r.id.soundbutton);     btnstop = (button) findviewbyid(r.id.stopbutton);      mpsound = mediaplayer.create(this, r.raw.a);     mpsound.setlooping(false);     btnplay.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {                 //mpsound.start();                 uri uri = uri.parse("android.resource://com.test.testing/" + r.raw.a);                 //toast.maketext(getapplicationcontext(), uri.tostring(), 2000).show();                 playsong(uri.tostring());                 btnplay.setvisibility(view.gone);                 btnstop.setvisibility(view.visible);                 btnstop.setonclicklistener(stopsound);         }     });  view.onclicklistener stopsound = new view.onclicklistener() {     public void onclick(view v) {         if (mpsound != null) {             mpsound.stop();             //mpsound.release();             btnplay.setvisibility(view.visible);             btnstop.setvisibility(view.gone);         }     } }; 

the playsong() function is:

    private void playsong(string songpath) {         try {                  mpsound.reset();                 mpsound.setdatasource(songpath);                 mpsound.prepare();                 mpsound.start();                  //stop song , replace button                 mpsound.setoncompletionlistener(new oncompletionlistener() {                         public void oncompletion(mediaplayer arg0) {                             mpsound.stop();                             btnplay.setvisibility(view.visible);                             btnstop.setvisibility(view.gone);                         }                  });          } catch (ioexception e) {                 log.v(getstring(r.string.app_name), e.getmessage());         } } 

my sound located in res/raw/a.mp3

when press btnplay button displayed btnstop button nothing playing.

i looking let user play , stop , play , forth. how can accomplish it?

logcat:

08-23 15:41:20.761: e/androidruntime(29438): fatal exception: main 08-23 15:41:20.761: e/androidruntime(29438): java.lang.runtimeexception: unable instantiate activity componentinfo{com.test.testing/com.test.testing.alpdisplay}: java.lang.nullpointerexception 08-23 15:41:20.761: e/androidruntime(29438):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2222) 08-23 15:41:20.761: e/androidruntime(29438):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2356) 08-23 15:41:20.761: e/androidruntime(29438):    @ android.app.activitythread.access$600(activitythread.java:150) 08-23 15:41:20.761: e/androidruntime(29438):    @ android.app.activitythread$h.handlemessage(activitythread.java:1244) 08-23 15:41:20.761: e/androidruntime(29438):    @ android.os.handler.dispatchmessage(handler.java:99) 08-23 15:41:20.761: e/androidruntime(29438):    @ android.os.looper.loop(looper.java:137) 08-23 15:41:20.761: e/androidruntime(29438):    @ android.app.activitythread.main(activitythread.java:5195) 08-23 15:41:20.761: e/androidruntime(29438):    @ java.lang.reflect.method.invokenative(native method) 08-23 15:41:20.761: e/androidruntime(29438):    @ java.lang.reflect.method.invoke(method.java:511) 08-23 15:41:20.761: e/androidruntime(29438):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) 08-23 15:41:20.761: e/androidruntime(29438):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:562) 08-23 15:41:20.761: e/androidruntime(29438):    @ dalvik.system.nativestart.main(native method) 08-23 15:41:20.761: e/androidruntime(29438): caused by: java.lang.nullpointerexception 08-23 15:41:20.761: e/androidruntime(29438):    @ android.content.contextwrapper.getresources(contextwrapper.java:89) 08-23 15:41:20.761: e/androidruntime(29438):    @ android.view.contextthemewrapper.getresources(contextthemewrapper.java:78) 08-23 15:41:20.761: e/androidruntime(29438):    @ android.media.mediaplayer.create(mediaplayer.java:824) 08-23 15:41:20.761: e/androidruntime(29438):    @ com.test.testing.musicplayer.<init>(musicplayer.java:13) 08-23 15:41:20.761: e/androidruntime(29438):    @ com.test.testing.alpdisplay.<init>(alpdisplay.java:45) 08-23 15:41:20.761: e/androidruntime(29438):    @ java.lang.class.newinstanceimpl(native method) 08-23 15:41:20.761: e/androidruntime(29438):    @ java.lang.class.newinstance(class.java:1319) 08-23 15:41:20.761: e/androidruntime(29438):    @ android.app.instrumentation.newactivity(instrumentation.java:1054) 08-23 15:41:20.761: e/androidruntime(29438):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2213) 08-23 15:41:20.761: e/androidruntime(29438):    ... 11 more 

it can done simpler. i've tested code, it's working:

musicplayer.java:

public class musicplayer {     private final int songresid = r.raw.your_song;     private final mediaplayer mediaplayer;      public musicplayer(context context)     {         mediaplayer = mediaplayer.create(context, songresid);         mediaplayer.setlooping(true);     }      public void setplaying(boolean isplaying)     {         if (isplaying)         {             mediaplayer.start();         }         else         {             mediaplayer.stop();         }     }      public void release()     {         mediaplayer.stop();         mediaplayer.release();     } } 

usage (from activity):

musicplayer musicplayer = new musicplayer(this); musicplayer.setplaying(true);  // play musicplayer.setplaying(false); // stop musicplayer.setplaying(true);  // play beginning     musicplayer.release();         // when done using musicplayer 

make sure understand release().

for songs recommend ogg format (with maximum compression) on mp3, since it's taking 50% less space , quality indistinguishable. can convert file using example audacity.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -