cocos2d-iphone - 多个声音同时播放时音效中途停止?

标签 cocos2d-iphone audio simpleaudioengine

我有一个时长 8 秒的充电音效,仅在游戏中的每个关卡结束时触发播放。

问题是,当同时播放其他声音(激光、爆炸等)时,音效会中途停止。我在其他地方读到最多可以同时播放 32 个声音,但游戏最多可以同时播放 7 或 8 个音效。但充电音效还是断了。

如果我在游戏中不进行任何射击,而只播放充电效果,那么它会一直播放。

我在启动时预加载声音,如下所示:

-(void)setupSound
{
    [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"Kickshock.mp3" loop:YES];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"explosion_large.caf"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"explosion_small.caf"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"laser_ship.caf"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"powerup.mp3"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"railgun.mp3"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"metal.mp3"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"count.mp3"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"charge_up.mp3"];
}

-(id)init
{
    if( (self = [super init]) )
    {
        [self setupSound];
        // Rest of other code..
    }
    return self;
}

我会在需要时播放它们(发射激光、关卡结束、物体爆炸等):

[[SimpleAudioEngine sharedEngine] playEffect:@"charge_up.mp3" pitch:1.0 pan:0.0 gain:0.4];

我猜充电声音正在失去它的“槽位”,因为其他声音正在播放?就像上面提到的,当充电音效切断时,我最多只能同时播放 7 或 8 个声音。

如果我的声音失去了它的“槽位”,有没有办法可以锁定特定的音效,使其不会失去它的“槽位”?

知道我做错了什么或者我可以采取什么措施来纠正这个问题吗?任何帮助是极大的赞赏! :D 感谢您的浏览。

最佳答案

要播放多种声音,我将使用 CDAudioManager 和 CDSoundEngine,而不是 SimpleAudioEngine。

CDSoundEngine允许多个 channel (最多32个),可以同时播放声音。

示例:

// create engine
CDSoundEngine * engine = [CDAudioManager sharedManager].soundEngine;

// assign groups
NSArray * groups = [NSArray arrayWithObjects:
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:1], nil];

[engine defineSourceGroups:groups];
[CDAudioManager initAsynchronously:kAMM_FxPlusMusicIfNoOtherAudio];

// load requests
NSMutableArray * requests = [[[NSMutableArray alloc] init] autorelease];

NSString * plist_sounds = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"Sounds.plist"]];
if (![[NSFileManager defaultManager] fileExistsAtPath:plist_sounds]) {
    plist_sounds = [[NSBundle mainBundle] pathForResource:@"Sounds" ofType:@"plist"];
}
NSDictionary * dictionary_sounds = [[NSDictionary dictionaryWithContentsOfFile:plist_sounds] objectForKey:@"Sounds"];   
if (dictionary_sounds != nil) {
    for (id key in dictionary_sounds) {
        [requests addObject:[[[CDBufferLoadRequest alloc] init:[key intValue] filePath:[dictionary_sounds objectForKey:key]] autorelease]];
    }
}

[engine loadBuffersAsynchronously:requests];

然后播放声音:

- (ALuint)play:(NSInteger)sound group:(NSInteger)group loop:(BOOL)forever volume:(float)volume {
     [[CDAudioManager sharedManager].soundEngine playSound:sound sourceGroupId:group pitch:1.0f pan:0.0f gain:volume loop:forever];
}

关于cocos2d-iphone - 多个声音同时播放时音效中途停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10050977/

相关文章:

iphone - SimpleAudioEngine 不播放

cocoa-touch - cocos2d游戏中播放声音

ios - 如何检测音效何时播放完毕?

iphone - 即使我暂停游戏,菜单也能正常工作吗?

swift - AKAmplitudeTracker振幅使用audioKit获得0.0

swift - SceneKit:位置音频不起作用

audio - 在 Cocos2D-X 中尝试启用音乐暂停时出现 "SimpleAudioEngine has not been declared"错误

iphone - 使用陀螺仪导航 View - iOS

ios - Swift:以编程方式对齐不同 IOS 屏幕尺寸的标签

javascript - 如何确定播放 HTML5 音频是否真的发出声音?