Android Game background music sound volume control

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.