0

I created class for handling sounds. But I've problem to stop sound? I can't send variable from playSound to stopSound AudioServicesDisposeSystemSoundID(soundID). How to handle this? I really don't want instantiate it or I've to?

@implementation SoundPlayer
+(void)playSound:(NSString *)filename ofType:(NSString *)extension {
    SystemSoundID soundID;
    NSString *soundFile = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
    AudioServicesPlaySystemSound(soundID); // error - soundID is undeclared
}

+(void)stopSound
{
    AudioServicesDisposeSystemSoundID(soundID);
}

@end

2 Answers 2

1

You'll either have to pass the id to stopSound (the same as when you play sound), or else if your audio player only plays 1 sound at a time, store the soundID as a static param on the class when you play.

Sign up to request clarification or add additional context in comments.

2 Comments

and how can I declare static param?
@interface TheClass : NSObject + (int)count; @end see: stackoverflow.com/questions/6035824/…
0

You can create soundID as ivar, something like:

@interface SoundPlayer(){
     SystemSoundID soundID;
}
@end

Other way use it as global variable

1 Comment

I tried it as a first option before posting to stackverflow. Instance variable accessed in class method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.