loop a sound at set intervals - objective-c

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.

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.

Related

Any bugs or changes in NSTimer with OC?

I'm testing NSTimer with OC and Swift. When I write with OC, here is something I don't quite understand: is it REQUIRED to add the timer to the NSRunLoop after the timer is inited? If not required, why can't it be invoked in a loop even if I set the repeats to YES?
Here is my code, I just init a timer and set repeats to YES, and what I expect is the code timerTick: should be invoked every 2 seconds, but it doesn't work as I expect... until I add the timer to the NSRunLoop.
OC:
-(void)viewDidLoad {
[super viewDidLoad];
NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
}
-(void) timerTick:(NSTimer*) timer{
NSLog(#"ticked,%#",#"aa");
}
I rewrote the same code with Swift
As you can see, I don't add the timer to NSRunLoop, however it works as I expected: the runTimeCode method is invoked every 2 seconds.
var timer:NSTimer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "runTimeCode", userInfo: nil, repeats: true)
}
func runTimeCode(){
NSLog("ticked")
}
My Question
Is there any bug in OC with iOS9.2 of NSTimer? I searched a lot with Google, but I didn't find anything saying it is REQUIRED if you want to let the timer works correctly.
How do I use NSTimer?
Is NSTimer usage different between OC and Swift?
First of all, you are using different methods. How come you would get the same result? scheduledTimerWithTimeInterval will automatically add to the NSRunLoop after initializing it. However, timerWithTimeInterval won't. You will need to manually call fire or add it to the NSRunLoop in order to trigger it.
Just like the methods' name mean, scheduledTimerWithTimeInterval will do scheduling for you. As for timerWithTimeInterval, it just gives you a timer. Apple is pretty restricted on the names. Sometimes, you can guess its purpose based on it.
With Objective-C, you should try this method :
NSTimer *timer = [NSTimer scheduleTimerWithTimeInterval:2 target:self userinfo:...];
You can see all method's content in Xcode.

How do I replace this NSTimer with a CADisplayLink?

I realize that a CADisplayLink would be better suited for the nature of my current project, however, i can't quite figure out how to implement a CADisplayLink and replace my NSTimer.
below is the code for my NSTimer
Movement = [NSTimer scheduledTimerWithTimeInterval:0.002 target:self selector:#selector(BarMoving) userInfo:nil repeats:YES];
how can I create a CADisplayLink that will perform the same function but more efficiently?
Create the thing:
_displayLink = [CADisplayLink displayLinkWithTarget:self
selector:#selector(BarMoving)];
Start it running:
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
... that'll cause your display link to issue calls to BarMoving on the main run loop (which is the one associated with the main thread and therefore the main queue) whenever that run loop is in the default mode. So things like when the user has their finger down scrolling a scroll view will pause your timer. NSTimer has the same default behaviour.

Cocoa How to display Informative Message

I need to display informative message box in Cocoa Application, which control should i use,
i read the document of NSAlert, but it seems, it will create the modal message box, where i need something, where i will just show a popup for fraction of seconds and will get destroyed after some time by it self.
Sounds like a job for Growl.
It's a third-party software product, for which we provide a framework you can include in your application. See also the application-developer page.
you could use the NSTimer for auto dismiss.
[self showMyMessage];//put your code in showMyMessage method to show your alert,
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self
selector:#selector(callToDismissAlert:) userInfo:nil repeats:NO];
After 60.0 second , iOS will call the below function
-(void) callToDismissAlert:(NSTimer*) t
{
[self dismissMyAlert];// put your code in dismissMyAlert method to dismiss your alert,
}

UiView fadeOut if not touched

I'd like to reproduce this behavior in my iPad application.
I have a subView that contains four custom buttons.
The view has an alpha value of 0.0
I have another custom button outside of the view descripted above that is always visible.
When the user touches the visible button the view appear animating its alpha to 1.0 showing the other 4 buttons.
Now I'like to start a timer that fires the view fadeOut after 2 seconds
When and if the user interact (say touchDown or whatever) with the buttons the timers need to be reset.
In other words the view may disappear only when nobody touches a button inside It.
Can you help me with this.
I've managed to learn the basics of UIView animation but I don't know how to queue them.
My iPad has iOS 3.2.2. installed.
Sorry for the bad explanation but this is my first iPad application and my first obj-c project.
You'd keep an NSTimer instance variable for that. As soon as your view is completely visible, which you can notice by e.g. implementing the fade in animation's delegate, you initialize it like this:
_fadeTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(fade:) userInfo:nil repeats:NO];
Make sure _fadeTimer is an instance variable, you need to be able to reset it. Then implement the fade out method:
- (void)fade:(NSTimer *)aTimer {
// Forget about timer!
_fadeTimer = nil;
[UIView beginAnimations:nil context:NULL];
// fade here
[UIView commitAnimations];
}
Upon every user interaction you just call a method that delays the fade. To do this, delete and re-create the timer. Or change it's fire date:
- (void)delayFade {
[_fadeTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow: 2.0]];
}
PS: There is no need to explicitly retain the timer. It's retained by the runloop until it fires. After the callback, it will be released anyways. Just make sure you always reset the variable to nil, otherwise your app may crash on an invalid access. If you need to delete the time beofre it fired, call the invalidate method.

Automatically Loading At App Start

[Cocoa/Objective-C]
I adapted a timer routine (for current time) from this site (and it works great - thanks). It's currently attached to a button.
My question is: How do I make it start when my app starts up (and not use a button) (in other languages, I'd simply put the action listener or timer in the Form)...?
Thank for any help on this!
In your application delegate you'll find a method called
- (void)applicationDidFinishLaunching:(UIApplication *)application
I guess that would be where to start a timer on app startup.
Put it in your awakeFromNib method. This is called on all objects that get deserialized from your nib (like your application delegate), but it isn't called until all objects are deserialized and wired (so you can use your text field, for instance). For example:
- (void)timerFired:(NSTimer*)timer
{
NSLog(#"Timer completed!");
}
- (void)awakeFromNib
{
[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector(timerFired:) userInfo:nil repeats:NO];
}
Obviously, in this simple example the timer could have been created in either the applicationDidFinishLaunching: method or the awakeFromNib method since it doesn't interact with any other serialized objects, but in your case, it sounds like you need the awakeFromNib method.