Use soundpool to place sound
When developing Android software, we may often need to play multimedia audio files. Generally, the mediaplayer class is used, but this class occupies a large amount of resources,
It may not be suitable for games and other applications. The soundpool class is described here. The soundpool class is in the SDK Android. Media. soundpool,
The name implies the meaning of the sound pool.It mainly plays short sound clips that can be loaded from program resources or file systems,
Compared with the mediaplayer class, you can use less CPU resources and reduce the response latency.
Compared with other audio playback categories, soundpool allows you to set parameters such as sound quality, volume, and playback ratio. And it can manage multiple audio streams at the same time,
Each stream has its own ID, which is used to manage an audio stream.. The basic usage of soundpool is as follows:
Create a soundpool object: New soundpool (INT maxstreams, int streamtype, int srcquality );
Load audio streams from resources or files: load (context, int resid, int priority );
Play Sound (INT soundid, float leftvolume, float rightvolume, int priority, int loop, float rate)
Private soundpool sndpool;
Private hashmap <string, integer> sndpoolmap = new hashmap <string, integer> ();
Final Static int ksoundmin = R. Raw. B1;
Final Static int ksoundmax = R. Raw. gameover;
Final Static int ksoundnum = ksoundmax-ksoundmin + 1;
Public void loadallsounds ()
{
If (sndpool = NULL)
Sndpool = new soundpool (ksoundnum, audiomanager. stream_system, 5 );
For (INT I = ksoundmin; I <= ksoundmax; I ++ ){
Loadsound (I );
}
}
Public void loadsound (int id)
{
String key = "" + ID;
If (sndpoolmap. containskey (key ))
{
System. Out. println ("Waring! The sound "+ ID +" has been loaded ");
Return;
}
Int Index = sndpool. Load (this. getcontext (), ID, 1 );
Sndpoolmap. Put ("" + id, index );
}
Public void playsound (int id)
{
Int Index = sndpoolmap. Get ("" + id );
Sndpool. Play (index, 1, 1, 0, 0, 1 );
}
In this way, you can use soundpool to play a sound. I believe many of my friends have already tried it,
However, soundpool is not perfect. Soundpool has some Design Bugs, which have not been fixed since Firmware Version 1.0,
We should be careful when using it. I believe Google will fix these problems in the future, but we 'd better list them as follows:
1. soundpool can only apply for a maximum of 1 MB of memory spaceThis means that we can only use some very short sound clips,
Instead of playing songs or playing background music.
2.Soundpool provides pause and stop methods, but these methods are recommended not to be used easily.,
Sometimes they may cause your program to be inexplicably terminated.
Android Development Network recommends that you do more test work when using these two methods, and some friends report that they will not immediately stop playing the sound,
Instead, the data in the buffer zone will be stopped after being played. It may take another second to play.
3.Soundpool efficiency issues. In fact, soundpool efficiency is good in these playing classes.,
However, some friends still test it in G1 with a latency of about ms, which may affect the user experience.
Maybe this does not care about soundpool itself, because the latency in droid with better performance is acceptable.
Soundpool has these defects at present, but it also has the irreplaceable advantages. Based on these, we recommend that you use soundpool in the following scenarios:
1. Audio Effect in the application (key-tone prompts, messages, etc)
2. Intensive and transient sounds in the game (such as the simultaneous explosion of multiple ships). Of course, you can think of more ways to use them.