开发者

loop a sound at set intervals

开发者 https://www.devze.com 2023-02-04 05:19 出处:网络
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.

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 th开发者_StackOverflow社区e same button again.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消