正在播放多个声音时音效中途停止?
我有一个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];
我guess猜测充电声音正在失去其“槽”,因为其他声音正在播放?如上所述,当充电音效切断时,我最多只能同时播放7或8个声音。
如果我的声音正在失去其“插槽”,是否有一种方法可以锁定特定的声音效果,使其不会丢失其“插槽”?
任何想法,我做错了什么,或者我可以做些什么来弥补这一点?任何帮助是极大的赞赏! :D感谢您的期待。
要播放多个声音,我会使用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];
}
好我是从看教程后想通了:
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实例?有没有人有任何链接/教程?谢谢!
对不起,我简单的英文...
简单的发动机必须在瞬间最多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