Is it possible to play sound with delay ?for example play sound and repeat it after 40seconds and repeat again
[NSTimer scheduledTimerWithTimeInterval:40.0 target:self selector:#selector(playSound:) userInfo:nil repeats:YES];
That will call the playSound: method every 40 seconds. If you'd rather wait exactly 40 seconds after the sound completes to start it again, you'll need to register a callback via AudioServicesAddSystemSoundCompletion or whatever API you're using to play the sound, and create a non-repeating timer in the callback function.
Then too, you could use performSelector:withObject:afterDelay: to avoid the explicit use of timers.
Related
I have a countdown TextField which I like to update every minute automatically in my iPhone app.
When the specific screen is visible it should count down - when switching to another screen the countdown obviously must not be updated.
When returning to the screen the countdown should continue again...
What would be the correct way of handling this?
Many thanks!
I would use an NSTimer. Here is the documentation for NSTimer.
Here is a snippet of code on how to set up a timer and get it to call a method:
NSTimer *theTimer = [NSTimer timerWithTimeInterval:60 target:self selector:#selector(updateTime) userInfo:nil repeats:NO];
This timer will call the updateTime method every time it fires. I have set it to NOT repeat, and instead, I would create a new timer each time I call the updateTime method. That way if you leave the UIViewController behind it will not keep firing the NSTimer.
Here is a generic updateTime method:
-(void) updateTime
{
//Update user interface
NSTimer *theTimer = [NSTimer timerWithTimeInterval:60 target:self selector:#selector(updateTime) userInfo:nil repeats:NO];
}
The timeInterval is in seconds, so this timer will keep firing every 60 seconds. You may wish to cut it a little shorter, like 30 seconds, or even 1 second and check the system time to see when you need to update your countdown.
I hope that helps!
Good Evening Fellas!
Is it possible for me to get a notification from an MPMoviePlayerController while it is playing a video. I mean, if i am able to get it every second, millisecond etc.
I have an indicator for the video current playback time that i want it updated continuously.
Thank you in advance.
The simplest way is going to be to set up a repeating timer yourself, and use that to update your display. e.g.:
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTime:) userInfo:nil repeats:YES];
sorry still a newbie in the programming. Basically I have an Uibutton, when that button is pressed it would switch to another view, when it switches to that view, a function which calls a telephone number and sends an SMS text message out starts. What I'm trying to do it make that function wait 30seconds before it starts.
Any idea how to do this please?
just use:
-performSelector:withObject:afterDelay:
YOu can use NSTimer for that.
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector(myTimerMethod:) userInfo:nil repeats:NO];
I'm trying to make an iOS app. As part of the app, I want a UIScrollView to scroll every X seconds. I believe that I need to use NSTimer. Is this correct?
Yes. You can use NSTimer for this:
float interval = 2.5f; //whatever interval you want
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:interval target:someTarget selector:#selector(someSelector:) userInfo:nil repeats:YES];
Yes. You can use NSTimer to perform either a delayed event or a periodic event. There is a good post on using NSTimer here.
Yes.
NSTimer* theTimer = [NSTimer scheduledTimerWithTimeInterval:X
target:someController
selector:#selector(scrollThatView)
userInfo:nil
repeats:YES];
You don’t need to use an NSTimer—there are other ways to do it—but yes, an NSTimer will allow you to do so.
I have a custom iPhone application which doesn't rely on UIKit in any way (it is not linked to UIKit). This means that I'm not using UIApplication and therefore not calling UIApplicationMain.
The problem is that when I create a timer it never fires. The timer is created like that:
[NSTimer timerWithTimeInterval:10 target:self selector:#selector(updateTime) userInfo:nil repeats:TRUE];
I am suspecting that the problem might be run-loop related as after I finish basic initialization in "main()" , I do this:
NSLog(#"Starting application ...");
applicationInstance = [MyApp alloc];
[applicationInstance setMainLayer:mainLayer];
[NSThread detachNewThreadSelector:#selector(runApplication) toTarget:applicationInstance withObject:nil];
CFRunLoopRun();
I reverse-engineered UIKit and noticed that UIApplication does much more things like registering for system events etc. NSTimer might be relying on one of those things. The other thing I've noticed is that in UIApplication's delegate the thread is the same as in main().
How do I get NSTimer to work properly? If it is run-loop related, can someone point out the error? "CFRunLoopRun()" is used to hang up the main thread so the rest of the application can run. I don't think it is right though.
Edit: I think the run loop needs to be configred. Somehow ...
Your problem is definitely run loop-related; see the NSTimer reference, specifically, the introduction:
Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide. Note in particular that run loops retain their timers, so you can release a timer after you have added it to a run loop.
The solution was pretty simple
NSTimer * timeUpdateTimer = [NSTimer timerWithTimeInterval:1 target:self selector:#selector(updateTime) userInfo:nil repeats:TRUE];
[[NSRunLoop mainRunLoop] addTimer:timeUpdateTimer forMode:NSDefaultRunLoopMode];
Thanks for the tip Shaggy Frog