I want to make a program that play sound in every hour but im stuck.I need some help to solve the problem, and that problem is that i dont know how to tell t开发者_开发知识库he program to play a sound in every hour.Im trying with some compare (i set the date to an integer and compare with an other integer), but this doesent seems to work...Can anybody help?(for ex.: i want that the NSDate plays me the sound az 13:00)Many thanx
NSTimer
is ok if you know the iOS app will be in the foreground when the timer expires. However to be more robust you'd need to use local nofications.
If you want to play a sound at a specified time without your app running in the foreground you'll have to use notifications:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = <some hour in the future>;
localNotif.repeatInterval = NSHourCalendarUnit;
localNotif.soundName = @"soundFile.caf";
localNotif.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
The sound file has to be part of your application main bundle though and you can't use arbritary sound files.
http://developer.apple.com/library/ios/#documentation/iphone/Reference/UILocalNotification_Class/Reference/Reference.html
Use NSTimer.
See Creating and Scheduling a Timer.
What you need is an : NSTimer
This tutorial could help.
Try this..
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(myTimerMethod:) userInfo:nil repeats:YES];
-(void)myTimerMethod
{
//Play an Alarm
}
You could use NSTimer
.
Setup NSTimer
NSTimer *timer = [[NSTimer alloc]initWithFireDate:<your_start_date>
interval:(60 * 60)
target:self
selector:@selector(timerHandler:)
userInfo:nil
repeats:YES];
and write your handler..
-(void)timerHandler:(NSTimer*)timer{
//play your sound here..
}
精彩评论