我使用NSTimer调用下面的函数,我想在倒计时结束时触发声音。我设法使用下面的代码在活动模式和后台进行。现在我想将它们包装在if语句中,这取决于应用程序状态,但它总是触发else
部分。
func CountDown() {
Timer -= 1
CountDownLabel.text = MakeTimeLabel()
ProgressCircle.strokeEnd = 1-(CGFloat(Timer/Test))
if Timer == -1 {
if UIApplicationDidEnterBackgroundNotification == true {
let soundnotification = UILocalNotification()
soundnotification.soundName = "Boxing.wav"
UIApplication.sharedApplication().scheduleLocalNotification(soundnotification)
println("timer is done in background mode")
} else {
// Load Sound
soundlocation = NSBundle.mainBundle().URLForResource("Boxing", withExtension: "wav")!
player = AVAudioPlayer(contentsOfURL: soundlocation, error: &Error)
player.volume = 1
// Play Sound
player.play()
println("timer is done in active mode")
}
StopButton(self)
}
}
提前致谢
答案 0 :(得分:0)
这是因为你正在比较String" UIApplicationDidEnterBackgroundNotification"使用布尔值" true"。这将始终被评估" false"。
我相信你需要比较:
UIApplication.sharedApplication().applicationState == UIApplicationState.Background