当播放多个声音时,声音效果会在中途停止?

时间:2012-04-07 01:31:40

标签: 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谢谢你的期待。

3 个答案:

答案 0 :(得分:1)

好的,我在看完教程之后弄明白了:

http://bobueland.com/cocos2d/?p=200

我没有意识到SimpleAudioEngine是用于短音效的。

对于那些陷入困境的人来说,这就是我所做的:

在界面中:

CDLongAudioSource *rightChannel;

在我的init或setupSound方法中:

rightChannel = [[CDAudioManager sharedManager] audioSourceForChannel:kASC_Right];
[rightChannel load:@"charge_up.mp3"];

当我想玩的时候:

[rightChannel play];

我确信这只会在你只有一个很长的音效时起作用。如果你有多个长音效,我相信你必须以不同的方式接近它。除非我只是创建更多的CDLongAudioSource实例?有没有人有任何链接/教程?谢谢!

答案 1 :(得分:1)

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

CDSoundEngine允许多个通道(最多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];
}

答案 2 :(得分:1)

抱歉我的简单英语...
简单引擎现在有最多32个音频通道

if(SoundId == 0)
{
    // when you say playEffect engine capture one channel for effect
    // do playEffect once and save result SoundID for Stop or Resume effect
    SoundId = SimpleAudioEngine::sharedEngine()->playEffect("audio.mp3");
}
// effect loaded, channel captured, say resume for play effect again, nothing more 
else
{
    SimpleAudioEngine::sharedEngine()->resumeEffect(SoundId);
}

// if you say effect nothing will happen with channel try
SimpleAudioEngine::sharedEngine()->stopEffect(SoundID);

在这种情况下,第一个效果的频道不会被新效果所取代, 如果运行次数超过32