Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-04-2011, 01:12 AM   #1 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default NSTimer and UIPicker sounds

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];
}

Last edited by aceiswild; 11-04-2011 at 02:36 AM.
aceiswild is offline   Reply With Quote
Old 11-04-2011, 08:26 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by aceiswild View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 11-05-2011, 12:57 AM   #3 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
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.
Thanks Duncan!
Now i just got to put things together. Now I'm receiving errors so slowly working them out. Should I Synthesize "theTimer"? I thought it could have been as simple as just checking which row was selected in the UIPicker and play the arraySound for that row.
aceiswild is offline   Reply With Quote
Reply

Bookmarks

Tags
array, picker, timer

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 417
17 members and 400 guests
7twenty7, Alex-alex, Apptronics RBC, baja_yu, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, matador1978, n00b, pbart, reficul, Retouchable, Sami Gh, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:18 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0