2

我有一个 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 感谢您的关注。

4

3 回答 3

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 实例?有没有人有任何链接/教程?谢谢!

于 2012-04-07T23:42:47.973 回答
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];
}
于 2012-04-10T18:40:27.093 回答
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,第一个效果的通道将不会被新效果替换

于 2012-05-02T12:56:54.680 回答