Android game background music audio volume control, android volume control

Source: Internet
Author: User

Android game background music audio volume control, android volume control

Game sound effects are the music we use to play games. This is also a must-have for every game. However, if the background music you play is interrupted, we can use getCurrentPosition () this method is used to determine the audio playback offset. In fact, this is also very simple. You only need to set (initialize the sound) and (load the sound resource) in the Code. Other codes are similar to those of the music player, such as starting and stopping. Let's talk about the sound effects in the Code:

[Code] [Java] Code

01 // audio volume

02 intstreamVolume;

03

04 // define the SoundPool object

05 privateSoundPoolsoundPool;

06

07 // define the HASH table

08 privateHashMapsoundPoolMap;

09

10 /************************************** *************************

11 * Function: initSounds ();

12 * Parameters: null

13 * Returns: None.

14 * Description: Initialize the sound system

15 * Notes: none.

16 *************************************** ************************/

17 publicvoidinitSounds (){

18 // initialize the soundPool object. The first parameter is the number of sound streams that can be played simultaneously. The second parameter is the sound type, and the third parameter is the sound quality.

19 soundPool = newSoundPool (100, AudioManager. STREAM_MUSIC, 100 );

20

21 // initialize the HASH table

22 soundPoolMap = newHashMap ();

23

24 // obtain the sound device and device volume

25 AudioManagermgr = (AudioManager) context. getSystemService (Context. AUDIO_SERVICE );

26 streamVolume = mgr. getStreamVolume (AudioManager. STREAM_MUSIC );

27}

28

29 /************************************** *************************

30 * Function: loadSfx ();

31 * Parameters: null

32 * Returns: None.

33 * Description: loads audio resources.

34 * Notes: none.

35 *************************************** ************************/

36 publicvoidloadSfx (intraw, intID ){

37 // load the sound effect in the resource to the specified ID (this ID will be used for playback)

38 soundPoolMap. put (ID, soundPool. load (context, raw, ID ));

39}

40

41 /************************************** *************************

42 * Function: play ();

43 * Parameters: sound: ID of the sound to be played, loop: number of cycles

44 * Returns: None.

45 * Description: playback sound

46 * Notes: none.

47 *************************************** ************************/

48 publicvoidplay (intsound, intuLoop ){

49 soundPool. play (soundPoolMap. get (sound), streamVolume, streamVolume, 1, uLoop, 1f );

50}


How does android add background music and sound effects to the game?

Use Service
 
I recently learned about android game development and helped design a music class, including background music and sound effects. I am a game developed based on the surfaceview framework.

It sounds awesome ..
 

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.