Cocos2d 播放2种不同的背景音乐文件或循环播放效果

Cocos2d play 2 different background music files or loop playEffect(Cocos2d 播放2种不同的背景音乐文件或循环播放效果)

本文介绍了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:

  1. 同时播放2个背景音乐文件,并停止与角色相关的一个
  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) loop message;
  • In this method, you'll need to find out what play effect normally does. The thing you're looking out for is the ALUInt that 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) handle that takes that in and passes it back to OpenAL to 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种不同的背景音乐文件或循环播放效果