Game Sound code for Android Development

Source: Internet
Author: User

// Sound volume

Int streamVolume;

// Define the SoundPool object private SoundPool soundPool;

// Define the private HashMap of the HASH table <Integer, Integer> soundPoolMap;

/*************************************** * ************************* Function: initSounds ();

* Parameters: null

* Returns: None.

* Description: Initialize the sound system.

* Notes: none.

**************************************** ***********************/

Public void initSounds () {// initialize the soundPool object. The first parameter is the number of sound streams that can be played simultaneously. The first parameter is the sound type, the third parameter is the sound quality soundPool = new SoundPool (100, AudioManager. STREAM_MUSIC, 100 );

// Initialize the HASH table soundPoolMap = new HashMap <Integer, Integer> ();

// Obtain the volume of the sound device and device. AudioManager mgr = (AudioManager) context. getSystemService (Context. AUDIO_SERVICE );

StreamVolume = mgr. getStreamVolume (AudioManager. STREAM_MUSIC );

}

/*************************************** * ************************* Function: loadSfx ();

* Parameters: null

* Returns: None.

* Description: loads audio resources.

* Notes: none.

**************************************** ***********************/

Public void loadSfx (int raw, int ID) {// load the sound effect in the resource to the specified ID (this is the ID when playing the video) soundPoolMap. put (ID, soundPool. load (context, raw, ID ));

}

/*************************************** * ************************* Function: play ();

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

* Returns: None.

* Description: playback sound

* Notes: none.

**************************************** ***********************/

Public void play (int sound, int uLoop) {soundPool. play (soundPoolMap. get (sound), streamVolume, streamVolume, 1, uLoop, 1f );}

Related Article

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.