使用的NSTimer在设定的时间间隔播放声音,在后台X code6.4不工作 [英] Using NSTimer to play sound at set intervals, not working in background Xcode6.4

查看:153
本文介绍了使用的NSTimer在设定的时间间隔播放声音,在后台X code6.4不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是全新的写作应用。

只是写了我的第一个应用程序为半程马拉松训练。这是一个运行/步行计时器,扮演一个运行或走的声音时,它的时间切换活动模式下,用户选择的时间间隔。
该应用程序工作正常,我的手机上时,它的活跃,但在后台停止。我经历了很多Stackflow帖子搜查,许多人说你不应该在后台使用的NSTimer。结果
喜欢一些帮助越来越这在后台工作。先谢谢了。

我用X code 6.4

我的code:

 定时器= NSTimer.scheduledTimerWithTimeInterval(1,目标:自我,选择:选择(计数),用户信息:无,重复:真)FUNC计数(){
    timerCount + = 1
    TCSEC =(timerCount%3600)60%
    tcMin =(timerCount%3600)/ 60
    tcHour =(timerCount / 3600)
    让strSec =字符串(格式为:%02D,TCSEC)
    让strMin =字符串(格式为:%02D,tcMin)
    让strHour =字符串(格式为:%02D,tcHour)
    totalTime.text =\\(strHour):\\(strMin):\\(strSec)    如果isRunning == TRUE {
        如果runCounter!= 0 {
            runCounter - = 1
            TCSEC =(runCounter%3600)60%
            tcMin =(runCounter%3600)/ 60
            tcHour =(runCounter / 3600)
            让strSec =字符串(格式为:%02D,TCSEC)
            让strMin =字符串(格式为:%02D,tcMin)
            让strHour =字符串(格式为:%02D,tcHour)
            timerLabel.text =\\(strHour):\\(strMin):\\(strSec)
        }其他{
            isRunning = FALSE
            activityLabel.text =走剩余时间
            playWalkSound()
            runCounter = runMax
                                        }}
    其他{
        如果walkCounter!= 0 {
            walkCounter - = 1
            TCSEC =(walkCounter%3600)60%
            tcMin =(walkCounter%3600)/ 60
            tcHour =(walkCounter / 3600)
            让strSec =字符串(格式为:%02D,TCSEC)
            让strMin =字符串(格式为:%02D,tcMin)
            让strHour =字符串(格式为:%02D,tcHour)
            timerLabel.text =\\(strHour):\\(strMin):\\(strSec)        }其他{            isRunning =真
            activityLabel.text =运行剩余时间
            playRunSound()
            walkCounter = walkMax
        }}}


解决方案

由于iOS的7你只有180秒的时间背景,但是当你点击主页按钮,你不把你的应用程序在后台运行。你让你的应用程序的暂停的背景。当你在后台code不运行。

据<一个href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html\"相对=nofollow>苹果:


  

当用户不主动使用应用程式,系统移动到背景状态。对于许多应用程序,背景状态只是在途中对应用程序短暂停留被暂停。悬浮的应用程式是改善电池的使用寿命,还使系统投入重要的系统资源给已经引起用户的注意的新前景的应用程序的一种方法


应用程序需要得到在后台运行的特别许可只是为了执行某些有限的活动,如位置,音响或蓝牙将保持它的活着的背景。

有一些窍门可以使用,但仍不允许应用程序超过〜180秒,只是实施 scheduledTimerWithTimeInterval:目标:选择:用户信息:重复:办法像往常一样,放在下面的的AppDelegate

  VAR backgroundUpdateTask:UIBackgroundTaskIdentifier = 0FUNC应用程序(应用程序:UIApplication的,didFinishLaunchingWithOptions launchOptions:[NSObject的:AnyObject]) -  GT;布尔{
   返回true
}FUNC applicationWillResignActive(应用程序:的UIApplication){
   self.backgroundUpdateTask = UIApplication.sharedApplication()。beginBackgroundTaskWithExpirationHandler({
       self.endBackgroundUpdateTask()
   })
}FUNC endBackgroundUpdateTask(){
    UIApplication.sharedApplication()。endBackgroundTask(self.backgroundUpdateTask)
    self.backgroundUpdateTask = UIBackgroundTaskInvalid
 } FUNC applicationWillEnterForeground(应用程序:的UIApplication){
   self.endBackgroundUpdateTask()
 }

在180秒计时器将不会触发更多的,当应用程序将复出前景将开始再次触发。

我强烈建议你阅读<一href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html\"相对=nofollow>后台执行
苹果的教程,你就可以知道它是如何工作,并填写您的需求。
我希望这帮助你。

I'm brand new to writing apps.

Just wrote my first app for training for a half marathon. It is a run/walk timer that plays a "Run" or "Walk" sound when it's time to switch activity mode, users select those intervals. The app works fine on my phone when it's active, but stops in the background. I searched through a lot of posts on Stackflow, and many said you should not use NStimer in the background.
Would love some help is getting this to work in the background. Thanks in advance.

I'm using Xcode 6.4

My code:

timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counting"), userInfo: nil, repeats: true)

func Counting(){
    timerCount += 1
    tcSec = (timerCount % 3600 ) % 60
    tcMin = (timerCount % 3600) / 60
    tcHour = (timerCount / 3600)
    let strSec = String(format: "%02d", tcSec)
    let strMin = String(format: "%02d", tcMin)
    let strHour = String(format: "%02d", tcHour)
    totalTime.text = "\(strHour):\(strMin):\(strSec)"

    if isRunning == true {
        if runCounter != 0 {
            runCounter -= 1
            tcSec = (runCounter % 3600 ) % 60
            tcMin = (runCounter % 3600) / 60
            tcHour = (runCounter / 3600)
            let strSec = String(format: "%02d", tcSec)
            let strMin = String(format: "%02d", tcMin)
            let strHour = String(format: "%02d", tcHour)
            timerLabel.text = "\(strHour):\(strMin):\(strSec)"
        } else {
            isRunning = false
            activityLabel.text = "Walk Time Left"
            playWalkSound()
            runCounter = runMax
                                        }}
    else {
        if walkCounter != 0 {
            walkCounter -= 1
            tcSec = (walkCounter % 3600 ) % 60
            tcMin = (walkCounter % 3600) / 60
            tcHour = (walkCounter / 3600)
            let strSec = String(format: "%02d", tcSec)
            let strMin = String(format: "%02d", tcMin)
            let strHour = String(format: "%02d", tcHour)
            timerLabel.text  = "\(strHour):\(strMin):\(strSec)"

        } else {

            isRunning = true
            activityLabel.text = "Run Time Left"
            playRunSound()
            walkCounter = walkMax
        }}

}

解决方案

Since iOS 7 you have only 180 seconds of background time, but when you tap the home button, you do not make your app run in the background. You make your app suspend in the background. Your code does not run when you are in the background.

According to Apple:

When the user is not actively using your app, the system moves it to the background state. For many apps, the background state is just a brief stop on the way to the app being suspended. Suspending apps is a way of improving battery life it also allows the system to devote important system resources to the new foreground app that has drawn the user’s attention.

Apps need get special permission to run in the background just in order to perform certain limited activities like location, audio or bluetooth which will keep it alive in background.

There is some trick you can use , but still don't allow the app more than ~180s, just implement scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: method as usual and put the following in your AppDelegate:

var backgroundUpdateTask: UIBackgroundTaskIdentifier = 0

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
   return true
}

func applicationWillResignActive(application: UIApplication) {
   self.backgroundUpdateTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
       self.endBackgroundUpdateTask()
   })
}

func endBackgroundUpdateTask() {
    UIApplication.sharedApplication().endBackgroundTask(self.backgroundUpdateTask)
    self.backgroundUpdateTask = UIBackgroundTaskInvalid
 }

 func applicationWillEnterForeground(application: UIApplication) {
   self.endBackgroundUpdateTask()
 }

After 180s the timer will not fire more, when the app will comeback in foreground it will start to fire again.

I strongly recommend you read the Background Execution tutorial of Apple to you can know how it works and fill your needs. I hope this help you.

这篇关于使用的NSTimer在设定的时间间隔播放声音,在后台X code6.4不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆