tags:

views:

118

answers:

5

Language - Objective-C

I'm using the SoundEffect class to play a short sound. I'm using an NSTimer to call a this method:

- (void)count {

count++;
if (count == 16) {
 NSBundle *mainBundle = [NSBundle mainBundle];
 SoundEffect *soundEffect = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"beep" ofType:@"aif"]];
 [soundEffect play];
 count = 0;
}
seconds = seconds - 0.0625; // is equal to 1/16th of a second; effectively takes off one second per second
if (seconds == 0) {
 [timer invalidate]; // cancels timer
 [timer release];
}

[self convertSeconds];

}

(The count thing is just so the sound plays at a certain interval.)

Anyway, everything works fine, but I think it's inefficient to keep allocating and initializing the SoundEffect class every time my timer fires (which is quite often). Is there a way in which I can aloc and init when I press a button to start the timer, and leave it allocated and initialized so all I have to do is [soundEffect play]?

Thanks!

A: 

Using static variable

You can use a static variable within the method like

- (void)count {
    static SoundEffect *soundEffect =
        [[SoundEffect alloc] initWithContentsOfFile:
            [mainBundle pathForResource:@"beep" ofType:@"aif"]];
    ...
}

Here soundEffect would be initialized only once at the first invocation of count and will remain so until the end of execution.

Deprecated: Using Singletons

EDIT: Removed singleton section. I don't think it's relevant to the question.

notnoop
Or he could just make soundEffect an instance variable in the class that holds the count method.
Terry Wilcox
@Terry: +1 Instance variables are better than singletons in my opinion.
notnoop
@Terry, I will try that out, thanks everyone!
+4  A: 

Maybe this link will help: Creating a Singleton Instance

ongle
+3  A: 

You can make a pointer to the SoundEffect class a member of your own class.

Emiel
A: 

You could initialize it in your appDelegate, and access it through there at any given time. Remember to dealloc it too.

Another solution could be to make a singleton of it

Phillip Jacobs
A: 

If it's a sound that you'll only be using inside the count method, you can use a static variable to do the trick (note that this is technically a memory leak, since the object is never released)

- (void)count {

count++;
if (count == 16) {
        NSBundle *mainBundle = [NSBundle mainBundle];
        static SoundEffect * soundEffect = nil;
        if (soundEffect == nil)
           soundEffect = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"beep" ofType:@"aif"]];
        [soundEffect play];
        count = 0;
}
seconds = seconds - 0.0625; // is equal to 1/16th of a second; effectively takes off one second per second
if (seconds == 0) {
        [timer invalidate]; // cancels timer
        [timer release];
}

[self convertSeconds];

This will only allocate the object once.

Eric