java - my soundpool project won't play properly? -
i've been trying implement soundpool class (with tutorial) when try run on virtual device, sound won't play... main_activity xml file loads fine, expected on loading, sound played.
here source code.
public class mainactivity extends activity { private soundpool soundpool; private boolean soundpoolloaded = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // create new soundpool instance. // 2 = number of sounds can simultaneously played // audiomanager.stream_music = type of sound stream. stream_music common 1 // 0 = sound quality, default 0. // setonloadcompletelistener loads file soundpool = new soundpool(2, audiomanager.stream_music, 0); soundpool.setonloadcompletelistener(new onloadcompletelistener() { @override public void onloadcomplete(soundpool soundpool, int sampleid, int status) { soundpoolloaded = true; } }); playsound("car_phonic"); } // method load sound resource res/raw folder input name given string (soundname). // if sound resource found name, attempt play sound // soundpoolloaded value becomes true when file loaded public void playsound(string soundname) { if(soundpoolloaded == true) { // current context resources , find correct sound resource playing sound resources res = this.getresources(); int soundid = 0; soundid = res.getidentifier(soundname, "raw", this.getpackagename()); if(soundid != 0){ soundpool.play(soundid, 1, 1, 0, 0, 1); } systemclock.sleep(500); playsound(soundname); } } }
any appreciated!
- play() takes soundid returned soundpools load(), not generic android resource id
- onloadcomplete called sound file finishes loading, notice sampleid argument
so, in it's basic form:
soundpool.setonloadcompletelistener(new onloadcompletelistener() { @override public void onloadcomplete(soundpool soundpool, int sampleid, int status) { soundpool.play(sampleid, 1, 1, 0, 0, 1); } }); soundpool.load(this, r.raw.car_phonic, 0);
note soundpool loads data in background samples not available.
Comments
Post a Comment