Introduction to audio file preprocessing using the audio CocosDenshion engine in Cocos2d-x,

Source: Internet
Author: User

Introduction to audio file preprocessing using the audio CocosDenshion engine in Cocos2d-x,
The Cocos2d-x provides an audio CocosDenshion engine that can be used independently of the Cocos2d-x. The CocosDenshion engine essentially encapsulates the OpenAL audio processing library.
The specific API used is SimpleAudioEngine. SimpleAudioEngine has several common functions:
Void preloadBackgroundMusic (const char * pszFilePath) pre-processes background music files and decompress compressed files, such as MP3 files and WAV Files.
Void playBackgroundMusic (const char * pszFilePath) to play background music.
Void stopBackgroundMusic () Stops Playing background music.
Void pauseBackgroundMusic () pause playing background music.
Void resumeBackgroundMusic () continues playing background music.
Bool isBackgroundMusicPlaying () determines whether the background music is playing.
Unsigned int playEffect (const char * pszFilePath.
Void pauseEffect (unsigned int nSoundId): pauses the playback effect. The nSoundId parameter is the ID returned by the playEffect function.
Void pauseAllEffects (): Pause all playing sound effects.
Void resumeEffect (unsigned int nSoundId) continues playing the sound. The nSoundId parameter is the ID returned by the playEffect function.
Void resumeAllEffects () continues playing all audio effects.
Void stopEffect (unsigned int nSoundId) to stop playing sound effects. The nSoundId parameter is the ID returned by the playEffect function.
Void stopAllEffects () to stop all playing sound effects.
Void preloadEffect (const char * pszFilePath) pre-processes audio files and decompress the compressed files, for example, MP3 files into WAV files.


Whether playing background music or sound effects, preprocessing is necessary before playing. This process is to decompress the audio file and process it, preprocessing only requires processing in sequence throughout the game operation. If preprocessing is not performed, you will find that the first time you play the audio file, it feels "stuck" and the user experience is poor.
Preprocessing functions include preloadBackgroundMusic and preloadEffect. The following code preprocessing background music and audio:
// Initialize background music
SimpleAudioEngine: getInstance ()-> preloadBackgroundMusic ("sound/jazz.pdf ");
// Initialize sound effects
SimpleAudioEngine: getInstance ()-> preloadEffect ("sound/Blip.wav ");
Where are the pre-processing process codes suitable? Since it is placed in any scenario layer, it is better not to place it in the scenario layer when it comes to this layer, it is better to place it in the AppDelegate file applicationDidFinishLaunching () in the function, the Code is as follows.
Bool AppDelegate: applicationDidFinishLaunching (){
... ...
// Run
Director-> runWithScene (scene );

// Initialize background music
SimpleAudioEngine: getInstance ()-> preloadBackgroundMusic ("sound/jazz.pdf ");
// Initialize sound effects
SimpleAudioEngine: getInstance ()-> preloadEffect ("sound/Blip.wav ");


Return true;
}

The applicationDidFinishLaunching () function is called back when the game starts. When a game is started, there is usually a startup interface, and the startup interface usually has a delay display, which is the best time for initialization.



More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): C ++ development" book exchange discussion site: http://www.c Ocoagame.netWelcome to cocos2d-x Technology Discussion Group: 257760386, 327403678




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.