This article describes the Android programming method to use SoundPool to play music. Share it for your reference, as follows:
If the application wants to play dense, short sound effects, it would be inappropriate to use MediaPlayer at this time.MediaPlayer has the following disadvantages:
1. High resource occupancy rate and long delay time
2. It does not support multiple audio playback at the same time
At this time, we can use SoundPool to play sound effects.SoundPool uses the concept of sound pool to manage multiple short sound effects, for example, it can start to load 20 sound effects, and then play it according to the sound effects ID in the program.
SoundPool is mainly used to play some shorter sound clips, with the advantages of low CPU resource usage and small response latency. It also supports setting the sound quality, volume, playback ratio and other parameters by yourself。
SoundPool provides a constructor that specifies how many sounds it supports in total, the quality of sound,
SoundPool(int maxStreams, int streamType, int srcQuality): The first parameter specifies how many sounds are supported, the second parameter specifies the type of sound, and the third parameter specifies the quality of sound
Once you get the SoundPool object, you can call multiple overloaded load methods of SoundPool to load the sound.
1. int load(Context context, int resId, int priority): Load the sound from the resource corresponding to resId
2. int load(FileDescript fd, long offset, long length, int priority): start to load the offset of the file corresponding to fd, and the sound of length is length
3. int load(AssetFileDescriptior afd, int priority): Load the sound from the corresponding file
4. int load(String path, int priority): load sound from the corresponding file of path
There is a priotity parameter in the above four methods. This parameter has no effect at present. Android recommends setting it to 1 to maintain compatibility with the future.
After the above four methods load the sound, they will return the ID of the changed sound. In the future, the program can play the specified sound through the ID of the sound. SoundPool provides the following method to play the sound:
int play(int sounded, float leftVolume, float rightVolume, int priority, int loop, float rate): The first parameter of this method specifies which sound to play, leftVolume, rightVolume specifies the left volume, and priority specifies the priority of playing the sound. The larger the value, the higher the priority. Loop specifies whether to loop, 0 is not loop, -1 is loop; rate specifies the playback ratio, the value can range from 0.5 to 2, and 1 is the normal ratio
In order to better manage each sound ID loaded by SoundPool, the program generally uses a HashMap<Integer,Integer> object to manage sounds
To summarize, the steps to play sounds using SoundPool are as follows:
1. Call SoundPool's constructor to create SoundPool object
2. Call the load() method of the SoundPool object to load sound from the specified resources and files. It is best to use HashMap<Integer,Integer> to manage the loaded sounds
3. Call SoundPool's play method to play sound
There is a problem with SoundPool:
1. SoundPool can only apply for a maximum of 1M memory space, which means we can only use some short sound clips instead of using it to play songs or game background music (the background music can be considered using JetPlayer to play).
2. SoundPool provides pause and stop methods, but it is recommended not to use these methods easily, because sometimes they may cause your program to terminate inexplicably. Some friends also reported that they would not abort the playback of sound immediately, but would stop playing the data in the buffer, and perhaps play for one second more.
3. It is recommended to use OGG format for audio format. In my little game Agile Buddy, I started by using WAV audio files to store game sound effects. After repeated testing, abnormal shutdown will occur when the sound effect playback interval is short (some say that SoundPool currently only has good support for 16-bit WAV files). Later, the file was converted to OGG format and the problem was solved.
Here is a point where you usually load music using SoudPool. If it is loaded when using it, you need to set the listening callback function setOnLoadCompleteListener( listener), otherwise there will be no sound during playback.
For more information about Android development, readers who are interested in reading the topic of this site:Android development introduction and advanced tutorial》
I hope this article will be helpful to everyone's Android programming design.