我需要有场景的背景音乐和角色的背景音乐,但是当角色做出一些动作时我必须停止它。
对于这个问题,我必须选择:
其中一个是2个可能&推荐?
问候!
答案 0 :(得分:16)
你说你正在使用cocos2d,所以我假设你也在使用cocos2d提供的SimpleAudioEngine
。
使用SimpleAudioEngine
时,无法播放2个背景音轨。可以通过一些小的修改来循环效果:
-(int) playEffect:(NSString*) file loop:(BOOL) loop
消息; ALUInt
用作声音的句柄。请参考这个。你需要它来阻止循环。-(void) stopEffectWithHandle:(int) handle
,然后将其传递回OpenAL
以停止效果。-EDIT -
以下是循环效果的一些代码:
int handle = [[SimpleAudioEngine sharedEngine] playEffect:name];
if (loop) {
alSourcei(handle, AL_LOOPING, 1);
}
return handle;
还有一些用于阻止效果:
[[SimpleAudioEngine sharedEngine] stopEffect:handle];
答案 1 :(得分:0)
我使用this forum post中的这种方法解决了这个问题。
播放效果:
CDSoundSource *loopSound = [[SimpleAudioEngine sharedEngine] soundSourceForFile:LOOPING_SOUND_FILENAME];
loopSound.looping = YES;
[loopSound play];
然后停止:
[loopSound stop];
希望这有帮助。