Cocos2d play 2 different background music files or loop playEffect(Cocos2d 播放2种不同的背景音乐文件或循环播放效果)
问题描述
我需要有一个场景的背景音乐和一个角色的背景音乐,但是当角色做出一些动作时我必须停止它.
I need to have to have a background music for the Scene and a background music for an Character,but I have to stop it when the character makes some actions.
对于这个问题我不得不选择:
For this problem I have to options:
- 同时播放2个背景音乐文件,并停止与角色相关的一个
- 循环播放音效.
这其中有 2 种可能 &推荐?
Which one of this is 2 possible & recommended?
问候!
推荐答案
你说你在使用cocos2d,所以我假设你也在使用cocos2d提供的SimpleAudioEngine.
You say you're using cocos2d so I've made the assumption that you're also using the SimpleAudioEngine provided with cocos2d.
使用 SimpleAudioEngine,无法播放 2 个背景音轨.可以通过一些小的修改来循环效果:
With SimpleAudioEngine, it's not possible to play 2 background tracks. It is possible to loop effects with some small modifications:
- 提供一个
-(int) playEffect:(NSString*) file loop:(BOOL) loop消息; - 在这种方法中,您需要了解通常的播放效果是什么.您正在寻找的是用作声音句柄的
ALUInt.保留对此的参考.您需要它来停止循环. - 提供一个
-(void) stopEffectWithHandle:(int) 句柄,将其接收并传递回OpenAL以停止效果.
- Provide a
-(int) playEffect:(NSString*) file loop:(BOOL) loopmessage; - In this method, you'll need to find out what play effect normally does. The thing you're looking out for is the
ALUIntthat is used as the handle for the sound. Keep a reference to this. You'll need it to stop the loop. - Provide a
-(void) stopEffectWithHandle:(int) handlethat takes that in and passes it back toOpenALto stop the effect.
-编辑-
下面是一些循环效果的代码:
Here's some code for looping an effect:
int handle = [[SimpleAudioEngine sharedEngine] playEffect:name];
if (loop) {
alSourcei(handle, AL_LOOPING, 1);
}
return handle;
还有一些用于停止效果:
And some for stopping effects:
[[SimpleAudioEngine sharedEngine] stopEffect:handle];
这篇关于Cocos2d 播放2种不同的背景音乐文件或循环播放效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Cocos2d 播放2种不同的背景音乐文件或循环播放效果
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
