1.3 KiB
1.3 KiB
name, description, type
| name | description | type |
|---|---|---|
| SoundManager Pattern | SoundPool implementation for low-latency audio on legacy Android | reference |
SoundPool Implementation for Galaxy S3
Why: MediaPlayer has too much latency for UI sounds. SoundPool is designed for low-latency playback.
How to apply:
public class SoundManager {
private static final int MAX_STREAMS = 8;
private SoundPool soundPool;
private Map<Integer, Integer> soundMap;
private void initializeSoundPool(Context context) {
soundMap = new HashMap<>();
if (Build.VERSION.SDK_INT >= 21) {
soundPool = new SoundPool.Builder()
.setMaxStreams(MAX_STREAMS)
.build();
} else {
// Legacy constructor for API 18-20
soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
}
}
public void playButtonClick() {
if (loaded && soundPool != null) {
Integer soundId = soundMap.get(SOUND_BUTTON);
if (soundId != null) {
soundPool.play(soundId, 0.7f, 0.7f, 1, 0, 1.0f);
}
}
}
}
Key points:
- Use application context to avoid leaks
- Implement as singleton
- Release resources in onDestroy
- Support both legacy and modern SoundPool APIs
- Use short OGG files (under 50KB)