cocos2dx 音频模块分析(3):背景音乐部分
我在(2)已经分析了背景音乐文件的预加载preloadBackgroundMusic和播放playBackgroundMusic两个函数,这里接着分析,还是基于androID平台:1、//暂停函数,用于音乐的暂停voID SimpleAudioEngine::pauseBackgroundMusic(){ //在SimpleAudioEngineJni.cpp源文件中定义 pauseBackgroundMusicJNI();}//--pauseBackgroundMusicJNI--->>>voID pauseBackgroundMusicJNI() { // voID pauseBackgroundMusic() JniMethodInfo methodInfo; if (! getStaticmethodInfo(methodInfo,"pauseBackgroundMusic","()V")) { return; } //通过jni调用java端的函数,调用的是Cocos2dxHelper类中的 /* public static voID pauseBackgroundMusic() { Cocos2dxHelper.sCocos2dMusic.pauseBackgroundMusic(); } */ methodInfo.env->CallStaticVoIDMethod(methodInfo.classID,methodInfo.methodID); methodInfo.env->DeleteLocalRef(methodInfo.classID); } 最终调用的是Cocos2dxMusic类中的 public voID pauseBackgroundMusic() { //mBackgroundMediaPlayer在(2)中有分析过,创建的MediaPlayer实例 if (this.mBackgroundMediaPlayer != null && this.mBackgroundMediaPlayer.isPlaying()) { this.mBackgroundMediaPlayer.pause(); this.mPaused = true; //是否暂停标志 } }2、恢复播放voID SimpleAudioEngine::resumeBackgroundMusic(){ resumeBackgroundMusicJNI();}其实和上面暂停的调用过程是一样的,就不分析了,直接进入java端看最终调用的函数,public voID resumeBackgroundMusic() { //这里只有处于暂停状态时即mPaused变量为true时,才会接着 //上次播放的位置开始播放 if (this.mBackgroundMediaPlayer != null && this.mPaused) { this.mBackgroundMediaPlayer.start(); this.mPaused = false; //把暂停标志位设置false } }3、从头开始播放音乐文件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端函数 //这个函数最终也是会调用playBackgroundMusic函数,但是和playBackgroundMusic有一点不同, //playBackgroundMusic需要传入音乐文件名,可以播放和上一次一样的音乐也可以和是上一次不一样的 //音乐,但是rewindBackgroundMusic函数只有在mBackgroundMediaPlayer不为null时才执行, //也就是必须播放过音乐,且播放的是上次播放的音乐,只不过这次是从头开始播放 public voID rewindBackgroundMusic() { if (this.mBackgroundMediaPlayer != null) { playBackgroundMusic(mCurrentPath,mIsLoop); } }4、停止播放音乐文件voID stopBackgroundMusicJNI() { // voID stopBackgroundMusic() JniMethodInfo methodInfo; if (! getStaticmethodInfo(methodInfo,"stopBackgroundMusic",methodInfo.methodID); methodInfo.env->DeleteLocalRef(methodInfo.classID); } --->>>//java端函数: public voID stopBackgroundMusic() { if (this.mBackgroundMediaPlayer != null) { mBackgroundMediaPlayer.release(); //不太明白这里为什么有从新创建了MediaPlayer实例 //可能是一些特殊情况下会出现问题? mBackgroundMediaPlayer = createMediaplayer(mCurrentPath); // should set the state,if not,the following sequence will be error // play -> pause -> stop -> resume //为什么设置mPaused标志,直接看上面的英文注释 this.mPaused = false; } }5、返回是否处于播放状态 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端函数,没什么东西 public boolean isBackgroundMusicPlaying() { boolean ret = false; if (this.mBackgroundMediaPlayer == null) { ret = false; } else { ret = this.mBackgroundMediaPlayer.isPlaying(); } return ret; }6、获取播放声音音量值 float getBackgroundMusicVolumeJNI() { // float getBackgroundMusicVolume() JniMethodInfo methodInfo; jfloat ret = -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、设置声音音量值 voID setBackgroundMusicVolumeJNI(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 = pVolume; if (this.mBackgroundMediaPlayer != null) { this.mBackgroundMediaPlayer.setVolume(this.mleftVolume,this.mRightVolume); } }8、end函数,这个一般在退出游戏是调用,关掉所有的音乐和音效。 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端函数 public static voID end() { Cocos2dxHelper.sCocos2dMusic.end(); //背景音乐文件的处理 Cocos2dxHelper.sCocos2dSound.end(); } ---->>>>/////背景音乐文件的处理 public voID end() { if (this.mBackgroundMediaPlayer != null) { this.mBackgroundMediaPlayer.release(); } //把所有的变量恢复初始值 /* private voID initData() { this.mleftVolume = 0.5f; this.mRightVolume = 0.5f; this.mBackgroundMediaPlayer = null; this.mPaused = false; this.mCurrentPath = null; } */ this.initData(); }总结
以上是内存溢出为你收集整理的cocos2dx 音频模块分析(3): 背景音乐部分全部内容,希望文章能够帮你解决cocos2dx 音频模块分析(3): 背景音乐部分所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)