SoundPool and play a few sounds, one after another

All your problems with Audio, Video and Images.

SoundPool and play a few sounds, one after another

Postby szakal » Thu Apr 15, 2010 9:23 am

Hello everybody,

What is the best way to play few sounds one by one, using SoundPool?

Maybe using another thread which play one sound, wait few seconds, play another one, wait few seconds etc.

This is my non-threading version. Any sugestion?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package pl.MYNAME.Sounds;
  3.  
  4.  
  5.  
  6. import java.util.HashMap;
  7.  
  8. import java.util.Vector;
  9.  
  10.  
  11.  
  12. import pl.MYNAME.R;
  13.  
  14. import android.content.Context;
  15.  
  16. import android.media.AudioManager;
  17.  
  18. import android.media.SoundPool;
  19.  
  20. import android.os.SystemClock;
  21.  
  22. import android.util.Log;
  23.  
  24.  
  25.  
  26. public class SoundsManager {
  27.  
  28.   public static final int FIRST = 0;
  29.  
  30.   public static final int SECOND = 1;
  31.  
  32.   public static final int THIRD = 2;
  33.  
  34.  
  35.  
  36.   private SoundPool soundPool;
  37.  
  38.   private HashMap<Integer, Integer> soundsMap;
  39.  
  40.  
  41.  
  42.   private Context context = null;
  43.  
  44.  
  45.  
  46.   public SoundsManager(Context context)
  47.  
  48.   {
  49.  
  50.     this.context = context;
  51.  
  52.     initSounds();
  53.  
  54.   }
  55.  
  56.  
  57.  
  58.   private void initSounds() {
  59.  
  60.     soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
  61.  
  62.     soundsMap = new HashMap<Integer, Integer>();
  63.  
  64.     soundsMap.put(FIRST, soundPool.load(context, R.raw.first, 1));
  65.  
  66.     soundsMap.put(SECOND, soundPool.load(context, R.raw.second, 1));
  67.  
  68.     soundsMap.put(THIRD, soundPool.load(context, R.raw.third, 1));
  69.  
  70.   }
  71.  
  72.    
  73.  
  74.   public void playSound(int sound) {
  75.  
  76.     AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
  77.  
  78.     int streamVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
  79.  
  80.     Integer s = soundsMap.get(sound);
  81.  
  82.     if (s!=null) {
  83.  
  84.       soundPool.play(s, streamVolume, streamVolume, 1, 0, 1f);
  85.  
  86.     }
  87.  
  88.   }
  89.  
  90.  
  91.  
  92.   public void play(int[] soundsId) {
  93.  
  94.     for (int i=0;i<soundsId.length;i++) {
  95.  
  96.       playSound(soundsId[i]);
  97.  
  98.           //wait until sound play
  99.  
  100.       SystemClock.sleep(800);
  101.  
  102.     }
  103.  
  104.   }
  105.  
  106.  
  107.  
  108.   public void example() {
  109.  
  110.     int soundsArray[] = {FIRST,THIRD,SECOND,THIRD,SECOND,FIRST};
  111.  
  112.         play(soundsArray);
  113.  
  114.   }
  115.  
  116.        
  117.  
  118. }
  119.  
  120.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


regards David
szakal
Freshman
Freshman
 
Posts: 3
Joined: Thu Apr 15, 2010 8:14 am
Location: 52.4164, 16.9243

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 21 guests