[英]Timed start objective-c
我想制作一个可以每小时播放声音但被卡住的程序。我需要一些帮助来解决问题,而那个问题是我不知道如何告诉程序每小时播放一个声音。比较(我将日期设置为一个整数,然后与另一个整数进行比较),但这似乎确实有效……有人可以帮忙吗?(例如:我希望NSDate播放1z声音给我)谢谢
NSTimer
is ok if you know the iOS app will be in the foreground when the timer expires. 如果您知道当计时器到期时iOS应用将位于前台,则
NSTimer
可以。 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 http://developer.apple.com/library/ios/#documentation/iphone/Reference/UILocalNotification_Class/Reference/Reference.html
See Creating and Scheduling a Timer . 请参阅创建和计划计时器 。
You could use NSTimer
. 您可以使用
NSTimer
。
Setup NSTimer
设置
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..
}
Try this.. 尝试这个..
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(myTimerMethod:) userInfo:nil repeats:YES];
-(void)myTimerMethod
{
//Play an Alarm
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.