Android soundpool unexpectedly making the application close with no message when lots of sounds played -


in application have makesound(int) method called when want sounds made @ different stages of game. have created audiomanager , soundpool variable public , intialized sound pool , audio manager in oncreate method.

audiomanager = (audiomanager) this.getsystemservice(this.audio_service); soundpool = new soundpool(5, audiomanager.stream_music, 0); 

my makesound(int) method below

 private void makesound(int w){     if(onvolume) {    //onvolume public boolean var set             final int volume = audiomanager.getstreamvolume(audiomanager.stream_system);         int id;         switch(w){             case 1:                 id = soundpool.load(this, r.raw.metalimpact, 1);                 break;             case 2:                 id = soundpool.load(this, r.raw.swoosh, 1);                 break;             case 3:                 id = soundpool.load(this, r.raw.win, 1);             case 4:                 id = soundpool.load(this, r.raw.win, 1);//second chance                 break;             case 5:                 id = soundpool.load(this, r.raw.fanfare, 1);//second chance                 break;             default:id = soundpool.load(this, r.raw.metalimpact, 1);         }         final int soundid = id;          soundpool.setonloadcompletelistener(new soundpool.onloadcompletelistener() {              public void onloadcomplete(soundpool soundpool, int sampleid,                                        int status) {                  soundpool.play(soundid, volume, volume, 0, 0, 1f);             }         });     } } 

this working fine when had 1 sound played. after set switch case stmt , had lot of sounds playing, noticed application run fine sometime, crash no message or anything. loading soundpool much?..please let me know i'm doing wrong. thankyou!!

you using wrong way implement logic.

firstly not required load sounds each time in makesound(int w) method. instead load sound first , manage them whether loaded or not in loadcompletelistener.

hashmap<integer, integer >loadedsound=new map<>(); loadedsound.put(1,soundpool.load(this, r.raw.metalimpact, 1)); loadedsound.put(2,soundpool.load(this, r.raw.swoosh, 1));  loadedsound.put(3,soundpool.load(this, r.raw.win, 1));   loadedsound.put(4,soundpool.load(this, r.raw.win, 1)); loadedsound.put(5,soundpool.load(this, r.raw.fanfare,1)); 

now have soundids return load method,use play sound as

switch(w) {   case w: soundpool.play(loadedsound.get(w), volume, volume, 0, 0, 1f);   break;   default:soundpool.play(loadedsound.get(1), volume, volume, 0, 0, 1f); } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -