我试图设置闹钟,但我想要而不是播放音乐 该应用程序将读取我创建的待办事项列表。
我正在使用AVSpeechSynthesizer类来执行语音功能而且工作正常但问题是,我使用Timer来安排任务,当应用程序进入后台时,计时器停止工作
这是我的代码:
class Speaking : NSObject,AVSpeechSynthesizerDelegate {
let speechSynthesizerObject = AVSpeechSynthesizer()
let voice = AVSpeechSynthesisVoice(language: "en-au")
override init() {
super.init()
speechSynthesizerObject.delegate = self
}
func speak(string: String) {
let utterance = AVSpeechUtterance(string: string)
utterance.rate = 0.5
utterance.voice = self.voice
speechSynthesizerObject.speak(utterance)
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
print("all done")
}
}
闹钟功能
func setAlaram(){
var date = self.appData.Alaram
if(date < Date()){
date = Calendar.current.date(byAdding: .day, value: 1, to: date)!
}
let calendar = Calendar(identifier: .gregorian)
var component = calendar.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)
component.second = 0
var modifiedDateTime = calendar.date(from: component)!
var snooz = Timer(fireAt: modifiedDateTime, interval: 0, target: self, selector: #selector(setSpeechRecognizeAsAlaram), userInfo: "first", repeats: false)
RunLoop.main.add(snooz!, forMode: RunLoopMode.commonModes)
}
我希望在时间崩溃时触发的功能
func setSpeechRecognizeAsAlaram( sender: Timer){
guard let data = loadAppData() else{ return }
self.appData = data
let numberOfTodo = String(appData.ToDoList.count)
var items = String()
var counter = 1
for item in appData.ToDoList {
items += String(counter) + ", " + item + ". "
counter += 1
}
let date = Date()
let dateformat1 = DateFormatter()
let dateformat2 = DateFormatter()
dateformat1.dateStyle = .full
dateformat2.dateFormat = "hh:mm a"
var sentence = String()
if(appData.ToDoList.count == 0){
sentence = " Good Morning! It’s" + dateformat2.string(from: date) + "!. And it’s time to wake up!. I’m disappointed in you today. You forgot to add in new “TO-DO” items to your list."
}
else{
sentence = "Good Morning! Its " + dateformat2.string(from: date) + "!. And its time to wake up. Today is " + dateformat1.string(from: date) + ". You have " + numberOfTodo + " items on your to-do list for the day. " + items + " Thats all!. I hope you have a productive day! "
}
let spk = Speaking()
spk.speak(string: sentence)
}
任何帮助将不胜感激
谢谢
答案 0 :(得分:2)
您是否阅读过Background Execution,Apples注意到如何在后台运行任务。
在后台获取用户注意权的部分。有一个使用本地通知设置闹钟的示例。
来自Apple的网站
清单3-2 安排警报通知
- (void)scheduleAlarmForDate:(NSDate*)theDate
{
UIApplication* app = [UIApplication sharedApplication];
NSArray* oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if ([oldNotifications count] > 0)
[app cancelAllLocalNotifications];
// Create a new notification.
UILocalNotification* alarm = [[UILocalNotification alloc] init];
if (alarm)
{
alarm.fireDate = theDate;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.soundName = @"alarmsound.caf";
alarm.alertBody = @"Time to wake up!";
[app scheduleLocalNotification:alarm];
}
}
击> <击> 撞击>
<强> 更新 强>
当然,UILocalNotification
已被弃用。新的替代品为UNNotificationRequest。