Cocos2d iphone 播放多个声音时音效中途停止?

Cocos2d iphone 播放多个声音时音效中途停止?,cocos2d-iphone,audio,simpleaudioengine,Cocos2d Iphone,Audio,Simpleaudioengine,我有一个8秒长的充电音效,只有在游戏的每一关结束时才会触发 问题是,当同时播放其他声音(激光、爆炸等)时,声音效果会在中途停止。我在其他地方读到,你可以同时播放多达32种声音,但游戏可能最多同时播放7或8种声音效果。但充电声音效果仍然中断 如果我在游戏中不做任何射击,让充电效应发挥作用,它会一直发挥作用 我在启动时预先加载声音,如下所示: -(void)setupSound { [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@

我有一个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;
}
我会在需要的时候玩它们(激光射击、关卡结束、东西爆炸等):

我猜充电声音正在失去它的“位置”,因为其他声音正在播放?如上所述,当充电音效关闭时,我最多只能同时播放7或8种声音

如果我的声音失去了它的“位置”,有没有一种方法可以锁定特定的声音效果,这样它就不会失去它的“位置”


你知道我做错了什么,或者我能做些什么来补救吗?非常感谢您的帮助!:D谢谢您的关注。

好的,我在以下网站上看了教程后找到了答案:

我没有意识到SimpleAudioEngine是为短音效设计的

对于那些在这一点上束手无策的人,以下是我所做的:

在界面中:

CDLongAudioSource *rightChannel;
在我的init或setupSound方法中:

rightChannel = [[CDAudioManager sharedManager] audioSourceForChannel:kASC_Right];
[rightChannel load:@"charge_up.mp3"];
当我想玩的时候:

[rightChannel play];

我相信只有当你只有一个长音效时,这才有效。如果你有多个长音效,我相信你必须以不同的方式处理。除非我只是创建更多CDLongAudioSource实例?有人有这方面的链接/教程吗?谢谢

要播放多个声音,我会将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];
}

对不起,我的简单英语…
简单引擎目前最多有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次

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);