This document discusses using SoundPool to play sounds in an Android application. It explains how to:
1. Create a SoundPool object specifying the maximum number of streams, stream type, and quality.
2. Load sound files into the SoundPool using load() to get a sound ID.
3. Play sounds using play(), providing the sound ID, left/right volumes, priority, loop setting, and playback speed.
The document also notes that sound files should be placed in res/raw and provides an example layout and Java code to load and play a sound when a button is clicked.
Android Lab Test : Managing sounds with SoundPool (english)
1. Develop on Android
Android Lab Test
www.AndroidLabTest.com
Youku
By Bruno Delb
www.weibo.com/brunodelb
i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn
http://i.youku.com/brunoparis
Weibo
Officialsite
Lesson : The sound, with SoundPool
2. The sound, with SoundPool
• In this lesson, you will learn to play sounds.
• For this, you will use the SoundPool.
3. The sound, with SoundPool
• Create a SoundPool with these parameters :
– the maximum number of streams,
– the type of stream (example : STREAM_MUSIC),
– the quality (not yet used).
SoundPool soundPool;
soundPool = new SoundPool (
2, // max streams
AudioManager.STREAM_MUSIC, // stream type
100 // quality (not used)
);
4. The sound, with SoundPool
• Load the sounds with the method load() with the parameters :
– the context,
– the resource id,
– the priority.
• As result, you get an id (soundId).
soundId = soundPool.load (
this, // context
R.raw.audiofile, // resource id
0 // priority
);
5. The sound, with SoundPool
• Play each sound with the method play() with these parameters :
– the sound id,
– the left and right volumes,
– the priority,
– the status if the sound should be played in loop or not,
– the speed playback.
soundPool.play (
soundId, // sound id
1f, // left volume (0.0-1.0)
1f, // right volume (0.0-1.0)
0, // priority (0 = the lowest)
0, // loop (0 = no, -1 = yes)
1f // rate (0.5-2.0)
);
6. Resources
• An audio file audiofile (any extension) should be present in
the directory resraw of the project.
11. Follow me on my channel PengYooTV …
On my Youku channel
http://i.youku.com/brunoparis
Who am I ?
Bruno Delb (www.delb.cn),
Author of the first french book of development of Java mobile application (2002),
Consultant, project manager and developer of social & mobile applications,
let’s talk about your needs ...
And on Weibo :
http://www.weibo.com/brunodelb