How Android uses soundpool to play sound effects
In Android development, we often use mediaplayer to play audio files, but mediaplayer has some shortcomings, such as high resource consumption, long delay time, and does not support multiple audio playback at the same time. These shortcomings determine that the use of mediaplayer is not ideal in some occasions, such as game development with relatively high requirements for time accuracy.
In game development, we often need to play some game sound effects (such as bullet explosion, object impact, etc.). The common characteristics of these sound effects are short, dense and low delay. In such a scenario, we can use soundpool instead of mediaplayer to play these sound effects.
Soundpool (Android. Media. Soundpool), as the name suggests, means sound pool. It is mainly used to play some short sound clips and support loading from program resources or file system. Compared with mediaplayer, soundpool has the advantages of low CPU resource consumption and low response delay. In addition, soundpool also supports self setting parameters such as sound quality, volume and playback ratio, and supports the management of multiple audio streams through ID.
As far as the known data are concerned, soundpool has some design bugs, some of which have not been repaired since firmware version 1.0. We should be careful in use. I believe Google will fix these problems in the future, but we'd better list them:
1. Soundpool can only apply for 1m memory space at most, which means that we can only use some very short sound clips instead of playing songs or making game background music.
2. Soundpool provides pause and stop methods, but these methods are recommended not to be used easily, because sometimes they may make your program terminate inexplicably. It is suggested to do as much testing as possible when using these two methods. Some friends reported that they will not stop playing the sound immediately, but will stop after playing the data in the buffer, perhaps for one more second.
3. The efficiency of soundpool. In fact, the efficiency of soundpool is very good in these playback classes, but some friends test it in G1 with a delay of about 100ms, which may affect the user experience. Maybe it doesn't matter the soundpool itself, because the delay is acceptable in droids with better performance.
At this stage, soundpool has these defects, but it also has irreplaceable advantages. Based on these, we suggest that soundpool be used more in the following situations: 1. Sound effects in applications (key prompt sound, messages, etc.) 2. Dense and short sounds in games (such as multiple spaceships exploding at the same time)
1. Introduction to relevant methods:
1) Construction method:
The soundpool (int maxstreams, int streamtype, int srcquality) parameters are:
① Specifies how many sounds are supported and the maximum number of streams allowed to exist simultaneously in the soundpool object.
② Specifies the sound type. The stream type can be divided into stream_ VOICE_ CALL,STREAM_ SYstem,STREAM_ RING,STREAM_ Music and stream_ There are four types of alarm. Defined in AudioManager.
③ Specify the sound quality (sampling rate conversion quality), which is generally directly set to 0!
The above construction method can be used in the lower version, and this construction method is outdated after API 21 (Android 5.0)! When using a soundpool. Builder, we need to instantiate soundpool by calling:
To use the above code, set the targetsdk version to be greater than or equal to 21! Moreover, if the minsdk version is less than 21, the following reminder will appear:
2) Introduction to common methods:
① Load sound resources:
Parameter introduction:
② Playback control:
The parameters are:
③ Resource release:
You can call the release () method to release the memory and resources occupied by all soundpool objects. Of course, you can also release them according to the sound ID!
3. Use code example:
Operation effect diagram:
When you click the button, "Duang" will appear. Here are two load methods, raw and assert!
Key codes:
MainActivity.java:
The code is very simple. In addition, if you click the last button, the soundpool will be released, and then other buttons will not be Duang~
4. Onloadcompletelistener monitors whether the sound file has been loaded
Well, this is a temporary thought. After writing another article, we suddenly think of it. The usage is also very simple. We can add onloadcompletelistener to the above code, rewrite the onloadcomplete() method, and finally set this thing for the soundpool object!
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.