Android multimedia apps play audio using soundpool

Because mediaplayer takes a lot of resources and does not support playing multiple audio at the same time, Android also provides another class for playing audio - soundpool. Soundpool is the audio pool, which can play multiple short audio at the same time and occupy less resources. Soundpool is suitable for playing key tones or message prompt tones in applications, and playing dense and short sounds in games, such as the sound of multiple aircraft explosions. Using SoundPool to play the audio, you first need to create the SoundPool object, then load the audio you need to play, and finally call the play () method to play the audio.

1. Create soundpool object

The soundpool class provides a construction method to create soundpool objects. The syntax format of the construction method is as follows: soundpool (int maxstreams, int streamtype, int srcquality); The parameter maxstreams is used to specify how many audio can be accommodated; The parameter streamtype is used to specify the sound type, which can be specified through the constants provided by the AudioManager class. Usually, stream is used_ MUSIC; The parameter srcquality is used to specify the audio quality. The default value is 0.

For example, to create a soundpool object that can hold 10 audio, you can use the following code: soundpool soundpool = new soundpool (10, AudioManager. Stream_music, 0);

2. Load the audio to be played

You can use the load () method to load the audio to be played. There are four syntax formats for the load () method: A. public int load (context, int resid, int priority); B. public int load (string path, int priority) for loading audio through the specified resource ID; C.public int load (assetfiledescriptor AFD, int priority); Used to load audio d.public int load (filedescriptor FD, long offset, long length, int priority) from the file corresponding to assetfiledescriptor; Used to load audio with length starting from offset in filedescriptor object

For example, to load the audio file ding.wav through the resource ID, you can use the following code: soundpool.load (this, r.raw.ding, 1);

3. Play audio

Call the play () method of the soundpool object to play the specified audio. The syntax format of the play () method is as follows: play (int soundid, float leftvolume, float rightvolume, int priority, int loop, float rate); The parameters are described as follows: soundid: used to specify the audio to be played, which is the audio returned through the load() method. Leftvolume: used to specify the volume of the left channel, with a value range of 0.0-1.0. Rightvolume: used to specify the volume of the right channel, with a value range of 0.0-1.0. Priority: used to specify the priority of playing audio. The higher the value, The higher the priority, loop: used to specify the number of cycles, 0 means no cycle, and - 1 means cycle rate: used to specify the rate, which is normally 1, the lowest is 0.5, and the highest is 2

For example, to play the audio file notify.wav saved in the audio resource, you can use the following code: soundpool.play (soundpool.load (manufacturing. This, r.raw. Notify, 1), 1,1);

The following is a small example to play audio through soundpool: the location of the audio file is shown in figure -10.12.a.jpg layout file, and four buttons (dog barking button, bird barking button, alarm ring button and laughter button) RES / layout / main.xml are realized

MainActivity:

The operation results are shown in the figure

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.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>