开发者

stop effect in cocos2d?

after so much searc开发者_运维百科hing , i couldnt find any solution to stopping effect in cocos2d.

my effect is playing a sound that been taken from database, so to stop that particular sound i have to to this :

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

BUT i got warning : stopEffect making integer from pointer without cast ..

why is that ? how can i stop all the sounds that being playing at once ??? or not a particular one ? any other way ?

thanks a lot .


ok you do this :

ALuint soundEffectID;

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


If you not have soundEffectID, you can do next. It helped me to resolve my problem.

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


Something more...

If you wanna stop all running sounds, then

[SimpleAudioEngine end];

but this will dealloc the sharedEngine too so u need to call "SharedEngine" in case wanna play sound again:)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜