Cocos2dx audio module analysis (5): cocos2dx Audio Effect
Cocos2dx audio module analysis (5): Audio Effect
The preload and playback functions of the sound effect section analyzed in the previous article. This article analyzes other functions: 1. Pause the sound effect in a video. // note that nSoundId is not a java-side soundID, but a streamID. // do not confuse the parameter name. Void SimpleAudioEngine: pauseEffect (unsigned int nSoundId) {response (nSoundId) ;}>> void pause=tjni (unsigned int nSoundId) {// void pauseEffect (int) define methodInfo; if (! GetStaticMethodInfo (methodInfo, "pauseEffect", "(I) V") {return;} methodInfo. env-> CallStaticVoidMethod (methodInfo. classID, methodInfo. methodID, (int) nSoundId); methodInfo. env-> DeleteLocalRef (methodInfo. classID) ;}---- >>// java-side method: // pStreamID this parameter is the StreamIDpublic void pauseEffect (final int pStreamID) that calls the unsigned int SimpleAudioEngine: playEffect) {/*** Pause a playback stream. pause a stream ** Pau Se the stream specified by the streamID. this is the * value returned by the play () function. (This value is the return value of the play function) If the stream is * playing, it will be paused. if the stream is not playing * (e.g. is stopped or was previusly paused), calling this * function will have no effect. (if it is in the playing status, it is paused. If it is not in the playing status, it is invalid.) ** @ param streamID a streamID returned by the play () function */this. mSoundPool. pause (pStreamID );} 2. Pause all sound effects void pauseAllEffectsJNI () {// void pauseAllEffects () JniMethodInfo methodInfo; if (! GetStaticMethodInfo (methodInfo, "pauseAllEffects", "() V") {return;} methodInfo. env-> CallStaticVoidMethod (methodInfo. classID, methodInfo. methodID); methodInfo. env-> DeleteLocalRef (methodInfo. classID) ;}---> java-side public void pauseAllEffects () {/*** Pause all active streams. // Pause all playing sound effects ** Pause all streams that are currently playing. this function * iterates through all the active streams And pauses any that * are playing. it also sets a flag so that any streams that * are playing can be resumed by calling autoResume (). */this. mSoundPool. autoPause ();} 3. Resume playing a sound effect void resumeEffectJNI (unsigned int nSoundId) {// void resumeEffect (int) JniMethodInfo methodInfo; if (! GetStaticMethodInfo (methodInfo, "resumeEffect", "(I) V") {return;} methodInfo. env-> CallStaticVoidMethod (methodInfo. classID, methodInfo. methodID, (int) nSoundId); methodInfo. env-> DeleteLocalRef (methodInfo. classID) ;}---> public void resumeEffect (final int pStreamID) {/*** Resume a playback stream. * Only a paused stream can resume playback. * Resume the stream specified by the streamID. this * is the value returned by the play () function. if the stream * is paused, this will resume playback. if the stream was not * previusly paused, calling this function will have no effect. ** @ param streamID a streamID returned by the play () function */this. mSoundPool. resume (pStreamID);} 4. void resumeAllEffectsJNI () {// void resumeAllEffects () Jn IMethodInfo methodInfo; if (! GetStaticMethodInfo (methodInfo, "resumeAllEffects", "() V") {return;} methodInfo. env-> CallStaticVoidMethod (methodInfo. classID, methodInfo. methodID); methodInfo. env-> DeleteLocalRef (methodInfo. classID) ;}---- >>> java-side function: public void resumeAllEffects () {// can not only invoke SoundPool. autoResume () here, because // it only resumes all effects paused by pauseAllEffects () // The comments here are clear if (! This. mPathStreamIDsMap. isEmpty () {final Iterator <Entry <String, ArrayList <Integer> iter = this. mPathStreamIDsMap. entrySet (). iterator (); while (iter. hasNext () {final Entry <String, ArrayList <Integer> entry = iter. next (); for (final int pStreamID: entry. getValue () {this. mSoundPool. resume (pStreamID) ;}}} 5. Stop playing a sound effect. The nSoundId here is also a pStreamID. You cannot resume playing a stopped sound through resume. Void SimpleAudioEngine: stopEffect (unsigned int nSoundId) {stopEffectJNI (nSoundId) ;}---> java: public void stopEffect (final int pStreamID) {/*** Stop a playback stream. ** Stop the stream specified by the streamID. this * is the value returned by the play () function. if the stream * is playing, it will be stopped. it also releases any native * resources associated with this stream. (resources will be released) If The stream is not * playing, it will have no effect. ** @ param streamID a streamID returned by the play () function */this. mSoundPool. stop (pStreamID); // remove record // remove for (final String pPath: this. mPathStreamIDsMap. keySet () {if (this. mPathStreamIDsMap. get (pPath ). contains (pStreamID) {this. mPathStreamIDsMap. get (pPath ). remove (this. mPathStreamIDsMap. get (pPath ). indexOf (pStreamID); break ;}}} 6. void stopAllEffectsJNI () {// void stopAllEffects () JniMethodInfo methodInfo; if (! GetStaticMethodInfo (methodInfo, "stopAllEffects", "() V") {return;} methodInfo. env-> CallStaticVoidMethod (methodInfo. classID, methodInfo. methodID); methodInfo. env-> DeleteLocalRef (methodInfo. classID) ;}--- >>> java @ SuppressWarnings ("unchecked") public void stopAllEffects () {// stop effects, stop playing all sound effects if (! This. mPathStreamIDsMap. isEmpty () {final Iterator <?> Iter = this. mPathStreamIDsMap. entrySet (). iterator (); while (iter. hasNext () {final Map. entry <String, ArrayList <Integer> entry = (Map. entry <String, ArrayList <Integer>) iter. next (); for (final int pStreamID: entry. getValue () {this. mSoundPool. stop (pStreamID) ;}}// remove records, clear the playback record this. mPathStreamIDsMap. clear ();} 7. Uninstall the loaded audio effect (important) // pszFilePath: audio file name void SimpleAudioEngine: unloadEffect (const char * ps ZFilePath) {std: string fullPath = getFullPathWithoutAssetsPrefix (pszFilePath); unload1_tjni (fullPath. c_str () ;}---> void unload=tjni (const char * path) {// void unloadEffect (String) JniMethodInfo methodInfo; if (! GetStaticMethodInfo (methodInfo, "unloadEffect", "(Ljava/lang/String;) V") {return;} jstring stringArg = methodInfo. env-> NewStringUTF (path); methodInfo. env-> CallStaticVoidMethod (methodInfo. classID, methodInfo. methodID, stringArg); methodInfo. env-> DeleteLocalRef (stringArg); methodInfo. env-> DeleteLocalRef (methodInfo. classID) ;}--- >>> public void unloadEffect (final String pPath) {// stop effects // first Stop final ArrayList <Integer> streamIDs = this. mPathStreamIDsMap. get (pPath); if (streamIDs! = Null) {for (final Integer pStreamID: streamIDs) {this. mSoundPool. stop (pStreamID) ;}} this. mPathStreamIDsMap. remove (pPath); // unload into tfinal Integer soundID = this. mPathSoundIDMap. get (pPath); if (soundID! = Null) {/*** Unload a sound from a sound ID. ** Unloads the sound specified by the soundID. this is the value * returned by the load () function. returns true if the sound is * successfully unloaded, false if the sound was already unloaded. ** @ param soundID a soundID returned by the load () function * @ return true if just unloaded, false if previusly unloaded * // uninstall the sound effect this. mSoundPool. unload (sou NdID); // remove this from mPathSoundIDMap. mPathSoundIDMap. remove (pPath );}} /*************************************** * ******** conclusion: the most important functions of the sound effect are preloadEffect, unloadEffect, playEffect, and stopAllEffects. The other functions are not very common. java has two important maps: private final HashMap <String, integer> mPathSoundIDMap = new HashMap <String, Integer> (); this is a map used to store all the loaded audio file paths and SoundID. One is: // sound path and stream ids map cocos2dx original annotation // a file may be played when times at the same time // so there is an array map to a file pathprivate final HashMap <String, arrayList <Integer> mPathStreamIDsMap = new HashMap <String, ArrayList <Integer> (); stores all playback audio file paths and the map of StreamIDs. Note: SoundID and StreamID are not concepts here. A sound file corresponds to only one SoundID, but multiple streamids. Because a sound file can be played multiple times (StreamID ), but it only needs to be loaded once (SoundID ). **************************************** ********/