Android (Android) game development Sound Code _android

Source: Internet
Author: User

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); }

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.