Game sound is the music we play in the game, this is a necessary part of each game, but there is the background music you do play a sense of intermittent, we can use GetCurrentPosition () This method to determine the sound playback of the offset. In fact, this is very simple. As long as we set (Initialize sound) and (load the sound resources) in the code, the other is similar to the music player code, like start, stop. Not much to say, let's take a look at the code for how to achieve the sound effect:
Code [Java] Code
Volume of 01//Sound
Intstreamvolume;
03
04//Define Soundpool Object
Privatesoundpoolsoundpool;
06
07//Define Hash table
Privatehashmapsoundpoolmap;
09
10/***************************************************************
*function:initsounds ();
*parameters:null
*returns:none.
*description: Initialize sound system
*notes:none.
16 ***************************************************************/
Publicvoidinitsounds () {
18//Initialize the Soundpool object, the first parameter is how many sound streams are allowed to play simultaneously, the 2nd parameter is the sound type, the third parameter is the quality of the sound
Soundpool=newsoundpool (100,audiomanager.stream_music,100);
20
21//Initialize hash table
Soundpoolmap=newhashmap ();
23
24//Get sound device and device volume
Audiomanagermgr= (Audiomanager) Context.getsystemservice (Context.audio_service);
Streamvolume=mgr.getstreamvolume (Audiomanager.stream_music);
27}
28
29/***************************************************************
*function:loadsfx ();
*parameters:null
*returns:none.
*description: Loading Sound resources
*notes:none.
35 ***************************************************************/
Publicvoidloadsfx (Intraw,intid) {
37//Load the sound from the resource into the specified ID (it will be played when it is played)
Soundpoolmap.put (Id,soundpool.load (Context,raw,id));
39}
40
41/***************************************************************
*function:play ();
*parameters:sound: Id,loop of the sound you want to play: number of cycles
*returns:none.
*description: Play Sound
*notes:none.
47 ***************************************************************/
Publicvoidplay (Intsound,intuloop) {
Soundpool.play (Soundpoolmap.get (sound), streamvolume,streamvolume,1,uloop,1f);
50}
Android Game background music sound volume control