Loading...
Logo
Processing Forum
Hi,
I've got a small audio test working using soundpool. The below code plays a note when hitting the menu button and is the basis for a larger application I've made.
Copy code
  1. import android.media.SoundPool;
  2. import android.content.res.AssetManager; 
  3. import android.media.AudioManager; 

  4. SoundPool soundPool;
  5. AssetManager assetManager;

  6. int sound1;

  7. void setup() {
  8.   
  9.   soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0); 
  10.   assetManager = this.getAssets();
  11.   try { 
  12.   sound1 = soundPool.load(assetManager.openFd("c2.ogg"), 0); 
  13.   } catch (IOException e) {
  14.    e.printStackTrace(); 
  15.   }
  16. }

  17. void draw() {  
  18. }

  19. void keyPressed() {
  20.   if(keyCode == MENU) {
  21.     soundPool.play(sound1, 1, 1, 0, 0, 1);
  22.   }
  23. }    

  24. public void onDestroy() {

  25.   super.onDestroy(); 
  26.   if(soundPool!=null) { 
  27.     soundPool.release();
  28.   }
  29. }
When I run this code on the emulator or a device it works as expected. But if I hit the home button, open a different application that uses audio, then go back to my test app the sound sometimes plays a few times then drops out while the rest of the app continues to work, with no errors in the console.

I may be clutching at straws here but do I need to add something to the AndroidManifest.xml to tell it to prioritise my audio? Otherwise could there be something wrong with the code? I suppose Processing converts the code to Android-friendly java and keeps some of the more complex activity/service/audio focus business behind the scenes.. which is great if it works, but this has got me stumped. Frustrating because the problem is quite intermittent and the rest of my little music app is working very well.

I've looked at the Android dev site but haven't found any answers there yet. Any ideas much appreciated.

Replies(1)

ok I've found that reducing the max number of streams from 20 to a smaller number (in my case 5 is all I need) pretty much fixes this so far. Maybe the system is reserving memory for too many streams and starts to steal resources from apps when many are open at once.. Really only guessing but at least it works now.