views:

124

answers:

1

hi guys,

i have a simple UIButton that, once clicked, plays a 1 second sound. i want to be able to click that button really fast and produce that sound as many times as i humanly can.

i currently have this up and running by including the and maybe that is where the culprit is... also, i am digging into apple's references and cannot find the info for how quick is a UIButton to respond to each event and how, if at all, i can control and manipulate this value.

should i switch to a different audio framework like the "audio toolbox" or is there a way for me to speed things up, or perhaps instruct a button to accept a 2nd and 3rd press while the action of the first press is still underway.

cheers!

~nir.

A: 

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.)

John Franklin
thanx john.i've tried the first solution you had offered and the interval between sounds is actually slower than before. to test things out i attached a 5 seconds sound to the button and tried clicking it within 2-3 seconds interval, expecting the sounds to overlap - well - they do not. the button is still waiting for the first sound to finish even though i had separated the control from the play method call.before i go head and dig into the array of working threads - what are your thoughts about this outcome? why won't the button allow more than one thread to fork?thanks again!
pengas
If it is not playing overlapping sounds, I would suspect the call to play the sound is waiting for prior calls to complete first.
John Franklin