Android programming to use SoundPool to play music method

  • 2021-01-22 05:23:59
  • OfStack

This article describes the Android programming to achieve the use of SoundPool music playback method. To share with you for your reference, as follows:

If your application is going to play dense, short sound effects, using MediaPlayer is not appropriate. MediaPlayer has the following disadvantages:

1. High resource occupancy rate and long delay time

2, do not support multiple audio at the same time

At this point we can use SoundPool to play the sound effects. SoundPool uses the concept of sound pools to manage multiple short sound effects. For example, it can load 20 sound effects initially and then play back the sound effects as ID in the program

SoundPool is mainly used to play some short sound clips. The advantages of CPU are low resource consumption and low response latency. Also support to set the quality of sound, volume, play ratio and other parameters.

SoundPool provides a constructor that specifies how many sounds it supports in total, the quality of the sounds,

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 the sound

Once we have the ES34en object, we can call multiple overloaded ES36en methods of ES35en 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): Load the corresponding file offset, length of length sound

3, int load(AssetFileDescriptior afd, int priority): Loading sound from the corresponding file

4, int load(String path, int priotity): Loads the sound from the file corresponding to path

Each of the above four methods has an priotity parameter, which is currently of no use. Android recommends setting it to 1 to maintain future compatibility

After the above four methods load the sound, they all return the modified ID, and then the program can play the specified sound through the ID of the sound. ES88en provides the following methods to play the sound:

int play(int sounded, float leftVolume,float rightVolume, int priotity, int loop, float rate): The first parameter of this method specifies which sound to play. leftVolume,rightVolume,priority specifies the left and right volume. priority specifies the priority of the sound to play. Loop specifies whether or not to loop, 0 for not to loop, -1 for loop; rate specifies the playback ratio, which can range from 0.5 to 2, with 1 being the normal ratio

In order to better manage each sound ID is loaded by SoundPool, program 1 will normally use 1 HashMap < Integer,Integer > Object to manage the sound

To summarize, the following steps are used to play sounds using SoundPool:

1. Create an object for SoundPool by calling the SoundPool constructor

2. Call the load() method of the SoundPool object to load the sound from the specified resource, file. It is better to HashMap < Integer,Integer > To manage the sound that is loaded

3. Call play method to play the sound

SoundPool has problems:

1. SoundPool has a maximum memory of 1M, which means we can only use a few short sound clips instead of playing songs or game background music (consider using JetPlayer for background music).

2. SoundPool provides pause and stop methods, but these methods are recommended not to be used lightly, because sometimes they may cause your program to terminate without any reason. Others have reported that they don't stop playing sound immediately, but wait until the data in the buffer is finished playing, perhaps adding a second.

3. The audio format is recommended to use OGG format. In my little game Agile Buddy, I started using an WAV audio file to store the game's sound effects. After repeated testing, there will be abnormal closure when the sound is played at short intervals (it is said that SoundPool currently only has good support for WAV file of 16bit). The problem was resolved when the file was later converted to OGG format.

1 point, shown here in like 1 when using SoudPool loading music onCreate () function to load inside, if you load them when, to set up to monitor the callback function setOnLoadCompleteListener (SoundPool. OnLoadCompleteListener listener), otherwise will be no sound when play

For more information on Android development, please check out the Android Introduction and Advanced Course.

I hope this article is helpful to Android program design.


Related articles: