我是Cocos2d框架的新手,我在主菜单场景中帮助弄清楚如何在循环中播放.wav文件。另外,我想在两个精灵碰撞时如何播放声音方面有所帮助。
由于
答案 0 :(得分:9)
在v3之前,您会在使用之前预先加载音频文件。例如,要预加载背景音乐(它准备播放但不加载整个内容,它是流式传输的):
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"sound.aifc"];
要玩它你会打电话:
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:"sound.aifc"];
要停止它你会打电话:
[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
对于声音效果(比如当两个物体碰撞时播放声音),你会先做预载(它会加载并缓存整个物体)做同样的事情:
[[SimpleAudioEngine sharedEngine] preloadEffect:@"sound.caf"];
然后在需要时播放特定实例:
unsigned int audioId = [[SimpleAudioEngine sharedEngine] playEffect:@"sound.caf"
pitch:1.0f
pan:0.0f
gain:1.0f];
要停止您要调用的特定实例:
[[SimpleAudioEngine sharedEngine] stopEffect:audioId];
对于效果,你必须在完成后卸载音频(你不需要的背景音乐):
[[SimpleAudioEngine sharedEngine] unloadEffect:@"sound.caf"];
如果您使用的是v3,则可以使用以下方式预加载,播放和停止背景音乐:
// Preload...
[[OALSimpleAudio sharedInstance] preloadBg:@"sound.aifc"];
// Play (and loop the music)...
[[OALSimpleAudio sharedInstance] playBgWithLoop:YES];
// To stop the music...
[[OALSimpleAudio sharedInstance] stopBg];
对于v3中的音频效果,您可以使用以下方式预加载声音效果:
ALBuffer* soundBuffer = [[OALSimpleAudio sharedInstance] preloadEffect:@"sound.caf"];
或者您可以异步执行:
__block ALBuffer* soundBuffer;
[[OALSimpleAudio sharedInstance] preloadEffect:@"sound.caf"
reduceToMono:NO
completionBlock:^(ALBuffer* buffer)
{
soundBuffer = buffer;
}];
或者您可以通过以下方式加载音频文件数组:
NSArray* soundFiles = @[@"sound1.caf", @"sound2.caf", @"sound3.caf"];
[[OALSimpleAudio sharedInstance] preloadEffects:soundFiles
reduceToMono:NO
progressBlock:^(NSUInteger progress,
NSUInteger successCount,
NSUInteger total)
{
if (successCount == total)
{
CCLOG(@"Sound files loaded!");
}
}];
播放音效:
[[OALSimpleAudio sharedInstance] playEffect:@"sound.caf" loop:NO];
如果您有音频缓冲区,可以采用其他方式播放音效:
[[OALSimpleAudio sharedInstance] playBuffer:soundBuffer
volume:1.0f
pitch:1.0f
pan:0.0f
loop:NO];
要卸载所有已加载和缓存的效果:
[[OALSimpleAudio sharedInstance] unloadAllEffects];
希望这有帮助。