I am using the SoundPool as follows:
Caching my sounds:
// load sound effects
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,
0);
soundPoolMap = new HashMap<Integer, Integer>();
AssetFileDescriptor afd;
try {
afd =
context.getAssets().openFd("sounds/piecemove.ogg");
soundPoolMap.put(SOUND_PIECEMOVE,
soundPool.load(afd, 1));
afd =
context.getAssets().openFd("sounds/piecestop.ogg");
soundPoolMap.put(SOUND_PIECESTOP,
soundPool.load(afd, 1));
afd =
context.getAssets().openFd("sounds/pieceattack.ogg");
soundPoolMap.put(SOUND_PIECEATTACK,
soundPool.load(afd, 1));
afd =
context.getAssets().openFd("sounds/pieceselect.ogg");
soundPoolMap.put(SOUND_PIECESELECT,
soundPool.load(afd, 1));
afd =
context.getAssets().openFd("sounds/selection.ogg");
soundPoolMap.put(SOUND_MENUSELECTION,
soundPool.load(afd, 1));
afd =
context.getAssets().openFd("sounds/pageturn.ogg");
soundPoolMap.put(SOUND_PAGETURN,
soundPool.load(afd, 1));
} catch (IOException e) {
e.printStackTrace();
}
Function to play sounds:
public static int playSound(Context context, int sound) {
AudioManager mgr = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr
.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
/* Play the sound with the correct volume */
return soundPool
.play(soundPoolMap.get(sound), volume, volume,
1, 0, 1f);
}
Playing a sound effect i.e when button is clicked:
Resources.playSound(context, Resources.SOUND_PIECESELECT);
However as I said the sound is lagging. What can I do to improve this?
All my sounds are OGG file format.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en