Create a method to play the sound separate from the button handler method. Let's say you call it playSound
. In your button handler, execute that method in the background with:
[self performSelectorInBackground:@selector(playSound) withObject:nil];
That does incur additional overhead to spawn off a thread before it can play the sound. If you want to speed it up a bit more, create a pool of worker threads and use:
[self performSelector:@selector(playSound)
onThread:nextThread
withObject:nil
waitUntilDone:NO];
To create the pool of worker threads:
-(void)stillWorking {
NSLog(@"Still working!");
}
-(void)workerThreadMain {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSTimer *threadTimer = [NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:@selector(stillWorking)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:threadTimer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
[pool drain];
}
-(NSArray*)createWorkerThreads {
NSMutableArray *threadArray = [[[NSMutableArray alloc] initWithCapacity:10]autorelease];
NSThread *workerThread;
for (i=0;i<10;i++) {
workerThread = [[NSThread alloc]initWithTarget:self
selector:@selector(workerThreadMain)
object:nil];
[workerThread start];
[threadArray addObject:workerThread];
}
return [NSArray arrayWithArray:threadArray];
}
(This code is untested and may require some debugging, but it should get you going in the right direction.)