Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop a sound at set intervals

I'm relativly new to this so please bear with me. I need to know how to loop a sound (or any object for that matter) at a definable interval. Something simple like, touch a button and a small sound file plays every x seconds until you touch another button or touch the same button again.

like image 920
Brandon Storms Avatar asked Apr 14 '26 07:04

Brandon Storms


1 Answers

NSTimer is what you're looking for to perform an action at specific time intervals.

Creating a simple timer to perform some action every 5 seconds would look something like this:

//This will start a repeating timer that will fire every 5 seconds
-(IBAction)startTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0
                                                 target:self
                                               selector:@selector(someAction:)
                                               userInfo:nil
                                                repeats:YES];
}

//The method the timer will call when fired
-(void)someAction:(NSTimer *)aTimer {
    //Do stuff here
}


-(IBAction)stopTimer {
    [self.timer invalidate];
}

As far as playing sounds go, iOS provides a lot of options. Fortunately Apple has provided plenty of good documentation on the different options available to you, how to choose the right one, and implement it.

like image 134
David Barry Avatar answered Apr 15 '26 21:04

David Barry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!