Quote:
Originally Posted by aceiswild
I have a few audio files in a UIPickerView and when i select a sound and push a button it plays fine. But now I'm trying to integrate an NSTimer... So basically you select the sound you want from the UIPicker and press the start button for the NSTimer and when it counts to zero it plays that selected sound. I can get it to work perfectly with a single audio file but I'm having some troubles with multiple files. They are all arrays.
Any suggestions of tips on how i can get this to function with multiple arrays? Anything will help! It has no errors when i run but crashes when the timer gets to zero.
Here is my starting point with the NSTimer and array selection.
Code:
-(void) countDown {
NSString* buf=[[NSString alloc] initWithFormat:@"%d", count];
count--;
countLabel.text = buf;
[buf release];
if(count == -1) {
NSString *path = [[NSBundle mainBundle] pathForResource: [arraySound objectAtIndex:1] ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path]
, &soundID);
AudioServicesPlaySystemSound (soundID);
countLabel.text = @" ";
label3.backgroundColor = [UIColor clearColor];
[myTimer invalidate];
}
}
-(IBAction)countDown:(id)sender {
count = 10;
label3.backgroundColor = [UIColor lightGrayColor];
myTimer =[NSTimer scheduledTimerWithTimeInterval: 1
target:self selector:@selector(countDown)
userInfo:nil
repeats:YES];
}
|
The method a timer calls should take a single parameter: the timer itself.
You should not use the same name from your action for the method that gets called when the timer fires. rename your method timerFired: and declare it like this:
Code:
-(void) timerFired: (NSTimer) theTimer;
You can ignore the timer parameter if you don't need it. If you need to pass data to a timer method, you have to ask the timer object for the userInfo.
I would add an NSLog statement to your timerFired method to make sure it's getting called.
Another thing I see: You are always fetching the object at index 1 in your arraySound array. If there is only 1 object in the array, you will crash. If the object in the array is not a string, you will crash.
I didn't look at your code super carefully. There may be other problems.