2011-08-28 70 views
6

經過這麼多的搜索,我無法找到任何解決方案在cocos2d中停止效果。停止在cocos2d中的效果?

我的播放效果是從數據庫中獲取聲音,所以要停止特定的聲音,我必須這樣:

[[SimpleAudioEngine sharedEngine] stopEffect:[NSString stringWithFormat:@"%@.wav",sound]]; 

但我得到警告:從指針stopEffect使得整數,未投..

這是爲什麼? 我該如何阻止所有正在播放的聲音?或不是特定的?任何其他方式?

非常感謝。

+1

好的我明白了: ALuint soundEffectID; soundEffectID = [[SimpleAudioEngine sharedEngine] playEffect:play]; [[SimpleAudioEngine sharedEngine] stopEffect:soundEffectID]; – Curnelious

+0

您應該將此作爲答案提交,並接受您的答案以將問題標記爲已解決。 –

回答

20

確定你這樣做:

ALuint soundEffectID; 

//to start 
soundEffectID=[[SimpleAudioEngine sharedEngine] playEffect:@"my sound"]; 
//to stop 
[[SimpleAudioEngine sharedEngine] stopEffect:soundEffectID]; 
8

如果沒有soundEffectID,你可以做下一個。它幫助我解決了我的問題。

static NSMutableArray *soundsIdArr; 

@implementation MusicAndSound 

//It must be run before using sound 
+(void)initSound 
{ 
    NSLog(@"initSound"); 

    soundsIdArr = [NSMutableArray arrayWithCapacity:0]; 
    [soundsIdArr retain]; 
} 

+(void)playSound:(NSString *)fileName 
{ 
    [[SimpleAudioEngine sharedEngine] setEffectsVolume:1.0]; 

    soundEffectID = [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]]; 

    [soundsIdArr addObject:[NSString stringWithFormat:@"%i", soundEffectID]]; 
} 

+(void)stopAllSounds 
{ 
    [[SimpleAudioEngine sharedEngine] setEffectsVolume:0.0]; 

    for (int i=0; i<[soundsIdArr count]; i++) 
    { 
     [[SimpleAudioEngine sharedEngine] stopEffect:[[soundsIdArr objectAtIndex:i] intValue]]; 
    } 

    [soundsIdArr removeAllObjects]; 
} 

- (void)dealloc 
{ 
    [soundsIdArr release]; 

    [super dealloc]; 
} 

@end 
0

更多的東西......

如果你想停止所有正在運行的聲音,然後

[SimpleAudioEngine end]; 

但這會的dealloc的sharedEngine也因此u需要的情況下,想叫「SharedEngine」再次播放聲音:)