Music sound
Next, let's add background music and sound effects to the game.
The first thing we need to know is that the Quick engine encapsulates a sound-related audio module that we can use to easily invoke sound-related APIs to control the sound. The audio module provides a wide range of methods and properties, such as preload, play, pause, stop, restore sound, and more.
We also need to know that the sound engine in the game is divided into two types: music (the background music), and sound (the game sound, that is, other than the background music).
Here are some common APIs for controlling background music:
Pre-loading a music file: audio.preloadmusic (filename)
It is pre-loaded before playing music and can be played without delay when the music needs to be played. Limited to hardware devices and operating systems, it is usually possible to preload only one music file, with the parameters of the music filename.
Play Music: Audio.playmusic (filename, isloop), parameters indicate the music file name, and whether to loop the music, true by default.
If the music file is not already loaded, the music file is loaded first, and then it starts playing.
Note: Even if the music volume is 0.0,audio.playmusic () the playback operation will still occur.
If you want to stop the music to reduce CPU usage, you should stop the music playback completely using the Audio.stopmusic () interface.
Pause Music Playback: Audio.pausemusic ()
Resume paused music: Audio.resumemusic ()
Stop playing music: Audio.stopmusic (isreleasedata), parameter isreleasedata indicates whether music data is released, default to True.
Set the volume of your Music: audio.setmusicvolume (volume)
The volume volume is between 0.0 and 1.0, 0.0 is completely muted, and 1.0 indicates a 100% volume.
Return volume value of music: Audio.getmusicvolume ()
The return value is between 0.0 and 1.0, 0.0 is completely muted, and 1.0 indicates a 100% volume
The API for controlling sound effects is similar to controlling the background music, such as audio.playsound (filename, isloop), Audio.setsoundsvolume (volume), and so on, so here we are no longer listed.
Back to our game, because its music sound does not need to be too much set up and change, so we can use no more than 20 lines of code to achieve the entire sound system production. As follows:
First, create a new sound folder under the Res folder, and place the required audio files in this folder. Then in the Myapp.lua file, we pre-load the desired music sound.
1Audio.preloadmusic ("Sound/background.mp3") 2Audio.preloadsound ("Sound/button.wav")3Audio.preloadsound ("Sound/ground.mp3")4Audio.preloadsound ("Sound/heart.mp3")5Audio.preloadsound ("Sound/hit.mp3")
Next, we can play the background music in the mainscene scene.
1 audio.playMusic("sound/background.mp3", true)
Also, add a function similar to the following to play the sound effect where you need to play the sound.
1 audio.playSound("sound/button.wav")
quick-cocos2d-x3.3 Study (20)---------Music Sound