Game sound is the music that we play when playing games, this is also a necessary part of each game, but there is you do play the background music intermittent feeling, we can use GetCurrentPosition () This method to judge the sound playback offset. In fact, this is also very simple. As long as we set the code (initialization sound) and (load sound resources) on it, and the other is similar to the music player's code, like beginning, stop. No more, let's take a look at how the code achieves sound effects:
1. Volume of sound effects
int streamvolume;
Define Soundpool object Private Soundpool Soundpool;
Define hash table private HashMap soundpoolmap;
/*parameters:null
* Returns:none.
* Description: Initialize the sound system
* notes:none.
***************************************************************/
2. Initialization of Soundpool
The public void Initsounds () {///initializes the Soundpool object, the first parameter is how many sound streams are allowed to play at the same time, the 2nd argument is the sound type, and the third parameter is the quality of the sound Soundpool = new Soundpool (100, Audiomanager.stream_music);
Initialize hash Table Soundpoolmap = new HashMap ();
Obtain sound equipment and device volume Audiomanager mgr = (audiomanager) context.getsystemservice (context.audio_service);
Streamvolume = Mgr.getstreamvolume (audiomanager.stream_music);
}
/*************************************************************** * FUNCTION:LOADSFX ();
* Parameters:null
* returns:none.
* Description: Load Sound Resources
* Notes:none.
***************************************************************/
3. Loading
public void Loadsfx (int raw, int ID) {//load the sound effects in the resource to the specified ID (play the same as the ID when playing) soundpoolmap.put (ID, soundpool.load ( Context, raw, ID));
}
/*************************************************************** * Function:play ();
* Parameters:sound: ID of the sound effect to play, loop: number of Cycles
* Returns:none.
* Description: Play sound
* notes:none.
/Public
void Play (int sound, int uloop) { Soundpool.play (Soundpoolmap.get (sound), Streamvolume, Streamvolume, 1, Uloop, 1f); }