当应用程序在前台并且屏幕被锁定时播放声音 - iOS [英] Playing a sound while app is in the foreground and the screen is locked - iOS

查看:259
本文介绍了当应用程序在前台并且屏幕被锁定时播放声音 - iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前言:我正在构建一个闹钟应用程序。许多其他警报应用程序(如闹钟专业版)能够在屏幕锁定且应用程序位于前台时播放警报。它们的警报可以无限制地播放,并且可以使用系统音量逐渐增加音量。他们也没有控制音乐控件(如果你打开多任务屏幕并滚动播放音频,你将看不到他们的图标)

Preface: I'm building an alarm clock app. Many other alarm apps such as Alarm Clock Pro are able to play an alarm while the screen is locked and the app is in the foreground. Their alarms can play for an unlimited amount of time and can progressively increase in volume using the system volume. They also do not take control of the music controls (if you open up the multitasking screen and scroll to playing audio, you will not see their icon)

我有一些麻烦再现该功能。

I am having some trouble reproducing that functionality.

要在应用程序位于前台时播放警报,我们会触发本地通知,效果很好。屏幕被锁定时,我的成功有限(应用程序处于非活动状态)

To play alarms while the app is in the foreground we fire Local notifications which works great. I've have some limited success while the screen is locked (the and the app is in the Inactive state)

我使用了以下方法:


  1. 当屏幕被锁定时,每秒使用后台任务运行 NSTimer 。我设法保持应用程序打开超过10分钟,但我无法播放声音。时间到了

  1. Run an NSTimer every second with a background task when the screen is locked. I managed to keep the app open past the 10 minute maximum but I was unable to play a sound. when the time came

使用 AVAudioPlayer 播放1秒静音。当声音结束时,重播声音并检查应用程序是否处于非活动(锁定屏幕)状态。如果它处于锁定屏幕状态并且是时候播放闹钟,请播放它。这里的问题是我必须使用

Play a 1 second silent sound using AVAudioPlayer. When the sound ends, replay the sound and check if the app is in the Inactive (locked screen) state. If it is in the locked screen state and it's time to play an alarm, play it. The problem here is I must use

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

以便在后台继续更新歌曲。该方法将在音乐控件中显示Wake,并将停止我们想要避免的任何当前播放的音乐。

in order to continue updating the song while it's in the background. That method will display Wake in the music controls and will stop any currently playing music which we want to avoid.


  1. A #1和#2的组合。当应用程序打开时,无限重复播放无声音。当屏幕被锁定时,触发计时器以检查其是否有时间发出警报。在播放闹钟时,将静音声音切换为闹钟声音。该系统最终将强制关闭应用程序

相关知识已帮助但尚未解决我的问题:

Related knowledge that has helped but hasn't solved my question:


  1. 如何在静音模式下播放音频&锁定在iOS 6中?

  2. iOS 5深度睡眠预防

  3. 应用程序在后台时中断后恢复代码执行

  4. 如何在背景iphone sdk中播放音乐

  1. How do I start playing audio when in silent mode & locked in iOS 6?
  2. iOS 5 deep sleep prevention
  3. Resuming execution of code after interruption while app is in background
  4. How to play music in background iphone sdk

摘要:当屏幕被锁定且应用程序位于前台时,我无法阻止该过程在10分钟后被杀死。

UPDATE

我最终使用 https://github.com/mruegenberg/MMPDeepSleepPreventer 让我在10分钟后播放声音秒。然而,这就像疯了一样吃掉电池。我需要找到更有效的解决方案。

I ended up using https://github.com/mruegenberg/MMPDeepSleepPreventer which let me play a sound after 10 minutes. However, this eats up battery like crazy. I need to find a more efficient solution.

更新2

我已下载应用程序商店中的一些 http://marcopeluso.com/ 应用程序。他是Deep Sleep Preventer的创造者。我下载了他的一些应用程序,并进行了一些仪器测试,并看到他的应用程序没有像我的应用程序那样消耗电池(他的博客上某处声称每小时大约2.5%)。我非常肯定我需要优化我的应用程序,电池耗尽问题将消失,我的问题将得到解决!

I downloaded some of http://marcopeluso.com/ apps from the app store. He is the creator of the Deep Sleep Preventer. I downloaded some of his apps and ran some instruments tests and saw his app was not draining battery as much as my app is (roughly 2.5% per hour as claimed somewhere on his blog). I am very certain that I need to optimize my app and the battery drain problems will disappear and my problems will be solved!

更新3
我最终使用 https://github.com/mruegenberg/MMPDeepSleepPreventer

我在它自己独立的xCode项目中运行它,当应用程序在后台时它只使用0.5%的cpu。事实证明这是我的应用程序吸吮CPU。所以它现在运作良好:)

I ran it in it's own separate xCode project and it only uses 0.5% of the cpu when the app is in the background. It turns out it was my app was sucking the cpu. So all it working well now :)

推荐答案

请参阅上面的回复。基本上虽然

See my response above. Basically though

我最终使用的是 https:// github。 com / mruegenberg / MMPDeepSleepPreventer

我在它自己独立的xCode项目中运行它,当应用程序在后台时它只使用0.5%的cpu。事实证明,这是我的代码杀死了cpu。一切都很好!

I ran it in it's own separate xCode project and it only uses 0.5% of the cpu when the app is in the background. It turns out it was my code that was killing the cpu. All is well now!

这篇关于当应用程序在前台并且屏幕被锁定时播放声音 - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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