in my application I am using 3 sounds, short [8kb, 28kb, 260kb wav files]
I need both of them played quite frequently. So I wrote a singleton class
and initialised all the sounds to SystemSoundID member variables on first init. Then i keep playing them instead of creating SystemSoundID every time.
Now my question is am I leaking here ?
I need to dispose these soundids, on the destruction, especially means when application ends, what would that place be ?
or should I always create SystemSoundID and dispose them by implementing a playFinished callback ?
2 of these sounds are for almost any event in my app so are being used frequently. I think every time creating SystemSoundID and disposing them would be a performance issue.
thanks in advance
CH
I need both of them played quite frequently. So I wrote a singleton class
and initialised all the sounds to SystemSoundID member variables on first init. Then i keep playing them instead of creating SystemSoundID every time.
Now my question is am I leaking here ?
I need to dispose these soundids, on the destruction, especially means when application ends, what would that place be ?
or should I always create SystemSoundID and dispose them by implementing a playFinished callback ?
2 of these sounds are for almost any event in my app so are being used frequently. I think every time creating SystemSoundID and disposing them would be a performance issue.
thanks in advance
CH
Code:
static MySoundClass *mySharedInstance = nil;
@implementation MySoundClass
- (void) playSound:int stype {
SystemSoundID sid = 0;
switch (stype) {
case 1:
sid = oneSoundId;
break;
case 2:
sid = twoSoundId;
break;
case 3:
sid = threeSoundId;
break;
}
// Use audio services to play the sound
AudioServicesPlaySystemSound(sid);
}
- (SystemSoundID) createSoundId:(NSString *)file {
//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] resourcePath],
file];
//declare a system sound id
SystemSoundID soundID = 0;
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
//Use audio sevices to create the sound
OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
if (error != kAudioServicesNoError)
NSLog(@"Error creating sound %@", file);
return soundID;
}
- (id) init {
self = [super init];
if (self != nil) {
tapSoundId = [self createSoundId:@"one.wav"];
shuffledSoundId = [self createSoundId:@"two.wav"];
finishedSoundId = [self createSoundId:@"three.wav"];
}
return self;
}
+ (AMUtility *)sharedInstance {
@synchronized(self) {
if (nil == mySharedInstance) {
[[self alloc] init]; // assignment not done here
}
}
return mySharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (nil == mySharedInstance) {
mySharedInstance = [super allocWithZone:zone];
return mySharedInstance; // assignment and return on first allocation
}
}
return nil; // on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; // denotes an object that cannot be released
}
- (void)release {
//do nothing
}
- (id)autorelease {
return self;
}
@end