NSTimer and UIPicker sounds

Joined
Nov 4, 2011
Messages
4
Reaction score
0
Points
1
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. I get the SIGABRT.

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];
}
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
What line is the crash occurring on? Knowing that is needed to diagnose the issue.

I'd also place the invalidate immediately at the start of the block it is in, not at the end.

You've hardcoded 1 in the objectAtIndex, but if there is only one object in the array, you'll get a crash. How does arraySound get filled?

I'm not sure what you are asking when you say "Any suggestions of tips on how i can get this to function with multiple arrays?". And I wonder what does "...multiple files. They are all arrays." mean.

A tip. You can get rid of the buf NSString variable by doing the following.
Code:
countLabel.text = [NSString stringWithFormat: @"%d", count];
 
OP
A
Joined
Nov 4, 2011
Messages
4
Reaction score
0
Points
1
Hi thank you for the reply. I have it all working now but my only problem is now when i select a sound from my uipicker and press the count down button it plays the wrong sound then what i have selected. Its really weird, like sometimes it plays "bell.wav" and i don't even have that file in my application anywhere!! So I'm trying to fix that.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top