Cocos2dx audio module analysis (3): background music, cocos2dx background music
Cocos2dx audio module analysis (3): Background Music
I have analyzed the pre-loaded preloadBackgroundMusic and playBackgroundMusic functions of the background music file in (2). The analysis is based on the android platform: 1. // pause function, pause void SimpleAudioEngine: pauseBackgroundMusic () {// In SimpleAudioEngineJni. define pauseBackgroundMusicJNI () ;}// -- pauseBackgroundMusicJNI >>>> void callback () {// void pauseBackgroundMusic () JniMethodInfo methodInfo; if (! GetStaticMethodInfo (methodInfo, "pauseBackgroundMusic", "() V") {return ;}// call java functions through jni, the/* public static void pauseBackgroundMusic () {Cocos2dxHelper. sCocos2dMusic. pauseBackgroundMusic ();} */methodInfo. env-> CallStaticVoidMethod (methodInfo. classID, methodInfo. methodID); methodInfo. env-> DeleteLocalRef (methodInfo. classID);} the final call is public void pauseBackgroundM in the Cocos2dxMusic class. Usic () {// mBackgroundMediaPlayer has been analyzed in (2). the created MediaPlayer instance if (this. mBackgroundMediaPlayer! = Null & this. mBackgroundMediaPlayer. isPlaying () {this. mBackgroundMediaPlayer. pause (); this. mPaused = true; // pause flag} 2. Resume playing void SimpleAudioEngine: resumeBackgroundMusic () {resumeBackgroundMusicJNI ();} is actually the same as the previously paused call process, without analysis, go directly to the java side to check the final called function. public void resumeBackgroundMusic () {// only when the mPaused variable is in the paused state is true, it will start playing at // The Last playback position if (this. mBackgroundMediaPlayer! = Null & this. mPaused) {this. mBackgroundMediaPlayer. start (); this. mPaused = false; // set the pause flag to false} 3. Play the music file void rewindBackgroundMusicJNI () {// void rewindBackgroundMusic () JniMethodInfo methodInfo; if (! GetStaticMethodInfo (methodInfo, "rewindBackgroundMusic", "() V") {return;} methodInfo. env-> CallStaticVoidMethod (methodInfo. classID, methodInfo. methodID); methodInfo. env-> DeleteLocalRef (methodInfo. classID) ;}--- >>// java-side function // This function will eventually call the playBackgroundMusic function, but it is a little different from playBackgroundMusic. // playBackgroundMusic requires passing in the music file name, you can play the same music as the last one or the last one. However, the rewindBackgroundMusic function is only available in mBackgroundMediaPlaye. R is executed only when it is not null, // that is, the music must have been played, and the last music is played, but this time it is played from the beginning public void rewindBackgroundMusic () {if (this. mBackgroundMediaPlayer! = Null) {playBackgroundMusic (mCurrentPath, mIsLoop) ;}4. Stop playing the music file void stopBackgroundMusicJNI () {// void stopBackgroundMusic () JniMethodInfo methodInfo; if (! GetStaticMethodInfo (methodInfo, "stopBackgroundMusic", "() V") {return;} methodInfo. env-> CallStaticVoidMethod (methodInfo. classID, methodInfo. methodID); methodInfo. env-> DeleteLocalRef (methodInfo. classID) ;}--- >>> // java end function: public void stopBackgroundMusic () {if (this. mBackgroundMediaPlayer! = Null) {mBackgroundMediaPlayer. release (); // I don't quite understand why a new MediaPlayer instance is created here. // it may be a problem in some special circumstances? MBackgroundMediaPlayer = createMediaplayer (mCurrentPath); // shocould set the state, if not, the following sequence will be error // play-> pause-> stop-> resume // Why do I set the mPaused flag? directly read the above English comment this. mPaused = false ;}} 5. Check whether the bool isBackgroundMusicPlayingJNI () {// boolean rewindBackgroundMusic () JniMethodInfo methodInfo; jboolean ret = false; if (! GetStaticMethodInfo (methodInfo, "isBackgroundMusicPlaying", "() Z") {return ret;} ret = methodInfo. env-> CallStaticBooleanMethod (methodInfo. classID, methodInfo. methodID); methodInfo. env-> DeleteLocalRef (methodInfo. classID); return ret ;}--- >>> // java-side function, nothing public boolean isBackgroundMusicPlaying () {boolean ret = false; if (this. mBackgroundMediaPlayer = null) {ret = false;} else {ret = this. mBac KgroundMediaPlayer. isPlaying ();} return ret;} 6. Obtain the playback sound volume value float getBackgroundMusicVolumeJNI () {// float getBackgroundMusicVolume () JniMethodInfo methodInfo; jfloret =-1.0; if (! GetStaticMethodInfo (methodInfo, "getBackgroundMusicVolume", "() F") {return ret;} ret = methodInfo. env-> CallStaticFloatMethod (methodInfo. classID, methodInfo. methodID); methodInfo. env-> DeleteLocalRef (methodInfo. classID); return ret;} ------- >>// java public float getBackgroundVolume () {if (this. mBackgroundMediaPlayer! = Null) {return (this. mLeftVolume + this. mRightVolume)/2;} else {return 0.0f ;}7. Set the void volume (float volume) {// void setBackgroundMusicVolume () JniMethodInfo methodInfo; if (! GetStaticMethodInfo (methodInfo, "setBackgroundMusicVolume", "(F) V") {return;} methodInfo. env-> CallStaticVoidMethod (methodInfo. classID, methodInfo. methodID, volume); methodInfo. env-> DeleteLocalRef (methodInfo. classID) ;}---- >>> java public void setBackgroundVolume (float pVolume) {if (pVolume <0.0f) {pVolume = 0.0f;} if (pVolume> 1.0f) {pVolume = 1.0f;} this. mLeftVolume = this. mRightVolume = pVol Ume; if (this. mBackgroundMediaPlayer! = Null) {this. mBackgroundMediaPlayer. setVolume (this. mLeftVolume, this. mRightVolume) ;}} 8. The end function is called when you exit the game. All music and sound effects are turned off. Void endJNI () {// void end () JniMethodInfo methodInfo; if (! GetStaticMethodInfo (methodInfo, "end", "() V") {return;} methodInfo. env-> CallStaticVoidMethod (methodInfo. classID, methodInfo. methodID); methodInfo. env-> DeleteLocalRef (methodInfo. classID) ;}--- >>> // java end function public static void end () {Cocos2dxHelper. sCocos2dMusic. end (); // process the background music file Cocos2dxHelper. sCocos2dSound. end () ;}---- >>>>//// Background Music File Processing public void end () {if (this. mBackgroundMediaPlayer! = Null) {this. mBackgroundMediaPlayer. release ();} // restore all variables to their initial values/* private void initData () {this. mLeftVolume = 0.5f; this. mRightVolume = 0.5f; this. mBackgroundMediaPlayer = null; this. mPaused = false; this. mCurrentPath = null;} */this. initData ();}