The soundpool game sound solution is developed for the graphic and logic parts of the game. However, the processing of music and sound effects is a great deal of effort. Fortunately, the solution is a perfect solution. We will share it with you to improve it together! At the beginning, I used the normal mediaplayer method, but this method is not suitable for game development because it is common to play multiple sound effects simultaneously in the game. Mediaplayer's friends should know that it does not support real-time playback of multiple sounds, and there will be more or less latency, and this delay is intolerable, especially in the fast continuous playback of sound (Such as the continuous click button), it will be very obvious, and 3 ~ will appear in a long time ~ Latency of 5 seconds ~~ -_-!~~Later I checked a lot of information and found a solution on a foreign website: soundpool
The Code is as follows: // Sound volume
Int streamvolume; // Define the soundpool object
Private soundpool; // Define the hash table
Private hashmap <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 second parameter is the sound type, and 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 sound device and device volume
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 ID is used to play the video during playback)
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 ); } Note:
1. I tested, Soundpool It can only be used for playing sound effects, because a sound that exceeds 5.6 seconds cannot be played, and loading a sound that exceeds 5.6 seconds will cause other sound playing problems.
2. I have checked the Google SDK documentation, although it is recorded in the soundpool But there is no description, and even the parameter represents nothing. I have seen some netizens on foreign websites saying that this is a test API and may be canceled in the official SDK, however, this method is retained at least in version 1.0. If you want to develop a game on G1, use it as needed ~ Don't worry about unsupported versions ~~ 3. Regarding the mediaplayer loop problem, I found that although mediaplayer provides built-in setlooping (true/false); this method There will be a significant pause in the intermittent loop. It should be caused by reloading or some initialization operations. Later, I decided not to use its built-in cyclic functions for the loop, instead, it uses another function. Number: getcurrentposition (). It can be used to detect the playback offset of the sound. I want it to manually seekto (position) when it is just finished or is about to be finished ); In this way, we can avoid intermediate pauses ~~ However, the program efficiency will be slightly reduced because it needs to be detected at any time. In order to ensure a smooth loop of sound, I have endured it! The Code is as follows: If (mmediaplayer. getcurrentposition ()> = 15800) { Mmediaplayer. seekto (50 ); } |