cocos2dx 音頻模組分析(5): 音效部分,cocos2dx音效

來源:互聯網
上載者:User

cocos2dx 音頻模組分析(5): 音效部分,cocos2dx音效

cocos2dx 音頻模組分析(5): 音效部分

我們上一篇中分析的音效部分的預先載入和播放函數,這一篇來分析下其他函數:1、暫停某個播放中的音效//注意這裡的nSoundId不是java端的soundID,而是streamID//不要被參數的名字迷惑了。void SimpleAudioEngine::pauseEffect(unsigned int nSoundId){    pauseEffectJNI(nSoundId);}-->> void pauseEffectJNI(unsigned int nSoundId)    {        // void pauseEffect(int)                JniMethodInfo methodInfo;                if (! getStaticMethodInfo(methodInfo, "pauseEffect", "(I)V"))        {            return ;        }                methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, (int)nSoundId);        methodInfo.env->DeleteLocalRef(methodInfo.classID);    }---->>//java端方法://pStreamID 這個參數,是調用unsigned int SimpleAudioEngine::playEffect()放回的StreamIDpublic void pauseEffect(final int pStreamID) {     /**     * Pause a playback stream. 暫停一個播放流     *     * Pause the stream specified by the streamID. This is the     * value returned by the play() function. (這個值是play函數的傳回值)If the stream is     * playing, it will be paused. If the stream is not playing     * (e.g. is stopped or was previously paused), calling this     * function will have no effect.(如果處於播放狀態則暫停,如果不處於播放狀態,則無效)     *     * @param streamID a streamID returned by the play() function     */this.mSoundPool.pause(pStreamID);}2、 暫停所有音效 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端     public void pauseAllEffects() {  /**     * Pause all active streams. //暫停所有現正播放的音效     *     * 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、 恢複播放某個音效 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.     * 只有處於暫停狀態的stream,才可以恢複播放。     * 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     * previously 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()                JniMethodInfo methodInfo;                if (! getStaticMethodInfo(methodInfo, "resumeAllEffects", "()V"))        {            return ;        }                methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);        methodInfo.env->DeleteLocalRef(methodInfo.classID);    }---->>>java端函數:public void resumeAllEffects() {// can not only invoke SoundPool.autoResume() here, because// it only resumes all effects paused by pauseAllEffects()// 這裡注釋的很清楚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、停止播放某個音效,這裡的nSoundId同樣是pStreamID,停止播放的音效是不能通過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. (會釋放對應的資源)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// 從屬記錄列表中移除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,停止所有音效的播放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,清空播放記錄this.mPathStreamIDsMap.clear();}7、卸載載入的音效(重要)//pszFilePath:音效檔案名稱void SimpleAudioEngine::unloadEffect(const char* pszFilePath){    std::string fullPath = getFullPathWithoutAssetsPrefix(pszFilePath);    unloadEffectJNI(fullPath.c_str());}--->> void unloadEffectJNI(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// 先停止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 effectfinal 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 previously unloaded     */// 卸載音效this.mSoundPool.unload(soundID);// 從mPathSoundIDMap中移除this.mPathSoundIDMap.remove(pPath);}}/************************************************總結:音效部分最重要的函數是preloadEffect,unloadEffect,playEffect,stopAllEffects其他的感覺不是很常用,java有兩個比較重要的map,一個是:private final HashMap<String, Integer> mPathSoundIDMap = new HashMap<String, Integer>();這個是用來存放所有載入的音效檔案路徑和SoundID的map。一個是:// sound path and stream ids map cocos2dx原本注釋// a file may be played many 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>>();用來存放所有播放的音效檔案路徑和StreamIDs的map。注意:這裡的SoundID和StreamID不是一個概念,一個音效檔案只對應一個SoundID,而卻可以對應多個StreamID,因為一個音效檔案可以播放多次(StreamID),但是只需要載入一次(SoundID).************************************************/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.