博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2dx 音频模块分析(5): 音效部分
阅读量:4215 次
发布时间:2019-05-26

本文共 7939 字,大约阅读时间需要 26 分钟。

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
>> iter = this.mPathStreamIDsMap.entrySet().iterator(); while (iter.hasNext()) { final Entry
> 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
> entry = (Map.Entry
>) 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
streamIDs = this.mPathStreamIDsMap.get(pPath); if (streamIDs != null) { for (final Integer pStreamID : streamIDs) { this.mSoundPool.stop(pStreamID); } } this.mPathStreamIDsMap.remove(pPath); // unload effect final 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
mPathSoundIDMap = new HashMap
();这个是用来存放所有加载的音效文件路径和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
> mPathStreamIDsMap = new HashMap
>();用来存放所有播放的音效文件路径和StreamIDs的map。注意:这里的SoundID和StreamID不是一个概念,一个音效文件只对应一个SoundID,而却可以对应多个StreamID,因为一个音效文件可以播放多次(StreamID),但是只需要加载一次(SoundID).************************************************/

转载地址:http://lmsmi.baihongyu.com/

你可能感兴趣的文章
Web前端学习笔记——JavaScript之正则表达式、伪数组、垃圾回收
查看>>
Web前端学习笔记——JavaScript 之继承、函数进阶
查看>>
Web前端学习笔记——JavaScript之面向对象游戏案例:贪吃蛇
查看>>
不做单元测试?小心得不偿失!嵌入式系统单元测试工具,自动生成测试用例
查看>>
一种实用的联网汽车无线攻击方法及车载安全协议
查看>>
光靠欺骗检测是不够的:对抗多目标跟踪的攻击
查看>>
基于微区块链的V2X地理动态入侵检测
查看>>
面向V2C场景的ADAS数字孪生模型构建方法
查看>>
Comma2k19数据集使用
查看>>
面向自动驾驶车辆验证的抽象仿真场景生成
查看>>
一种应用于GPS反欺骗的基于MLE的RAIM改进方法
查看>>
筑牢网络安全基座,安全护航经济数字化转型大会成功举办
查看>>
单元测试工具:单元测试的测试前置驱动条件
查看>>
汽车智不智能?“智能座舱”有话说
查看>>
自动驾驶汽车CAN总线数字孪生建模(一)
查看>>
自动驾驶汽车CAN总线数字孪生建模(二)
查看>>
自动驾驶汽车GPS系统数字孪生建模(一)
查看>>
自动驾驶汽车GPS系统数字孪生建模(二)
查看>>
上海控安入选首批工控安全防护能力贯标咨询机构名单
查看>>
自动驾驶汽车传感器数字孪生建模(一)
查看>>