Real-time sound effects, as the name implies, are short and fast sound effects. They are suitable for audio files with short playback time and rapid response. For example, when typing an input method, the "cool" sound is like this, the sound of playing chess is also...
This technology is inherited from Android. Media. soundpool
The soundpool class is used to play back the sound resources of an application. It loads the sound file into the memory and is in consideration of performance, this technique is generally used to play audio files that take less than 7 seconds.
Constructor soundpool (INT maxstreams, int streamtype, int srcquality)
- Maxstreams: this parameter is used to set the number of sound effects that can be played at the same time. If it is set to 3, a maximum of three sound effects can be played at the same time.
- Streamtype: Specifies the audio type. It is usually set to stream_music in the game.
- Srcquality: audio file quality. The default value is 0.
Load the audio file public int load (context, int resid, int priority)
- Context: The application context, that is, the activity that calls the application.
- Resid: Resource ID
- Priority: priority, which can be set to 1
Play public final int play (INT soundid, float leftvolume, float rightvolume, int priority, int loop, float rate)
- Soundid: ID of the music to be played
- Leftvolume: used to control the left audio volume
- Rightvolume: used to control the right audio volume
- Priority: priority, 0 lowest
- Loop: number of cycles. 0 indicates no loop, and-1 indicates permanent loop.
- Rate: playback speed. The value ranges from 0.5 to 2.0. 1 indicates normal speed.
Tentative public final void pause (INT streamid)
The following is an example of applying this instant sound effect.
Package COM. loulijun. quicksound; import Java. util. hashmap; import android. app. activity; import android. media. audiomanager; import android. media. soundpool; import android. OS. bundle; import android. view. view; import android. widget. button; public class quicksoundactivity extends activity {// create a sound pool private soundpool sp; hashmap <integer, integer> map; private button startone = NULL; private button stopone = NULL; private button starttwo = NULL; private button stoptwo = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // initialize the sound pool initsoundpool (); startone = (button) findviewbyid (R. id. startone); stopone = (button) findviewbyid (R. id. stopone); starttwo = (button) findviewbyid (R. id. starttwo); stoptwo = (button) findviewbyid (R. id. stoptwo); startone. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {playsound (); // play the first sound effect, loop it again}); stopone. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {// pause the first sound sp. pause (map. get (1) ;}}); starttwo. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {// play the second sound, loop twice playsound () ;}}); stoptwo. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {// tentatively set the second audio sp. pause (map. get (2) ;}}) ;}// method of sound pool initialization private void initsoundpool () {sp = new soundpool (5, audiomanager. stream_music, 0); map = new hashmap <integer, integer> (); map. put (1, SP. load (this, R. raw. one, 1); map. put (2, SP. load (this, R. raw. two, 1);} // method of playing sound public void playsound (INT sound, int number) {audiomanager AM = (audiomanager) This. getsystemservice (this. audio_service); // returns the current maximum alarmmanager volume float audiomaxvolume = aM. getstreammaxvolume (audiomanager. stream_music); // returns the volume of the current audiomanager object float audiocurrentvolume = aM. getstreamvolume (audiomanager. stream_music); float volumnratio = audiocurrentvolume/audiomaxvolume; SP. play (map. get (sound), // the ID of the music to be played volumnratio, // The left audio volume volumnratio, // The right audio Volume 1, // priority, 0 indicates the lowest number, // The number of cycles, 0: No Loops,-1: No endless loops; 1); // playback speed. The value ranges from 0.5 to 2.0, and 1 indicates the normal speed }}