检测iPhone中断类型
我可以使用 applicationWillResignActive 和 applicationDidBecomeActive 检测 iPhone 进入睡眠状态并从睡眠状态恢复。但我如何找出它是什么类型的中断。我正在制作一个音频播放器应用程序,需要在 iPhone 进入睡眠状态时保持音频播放(我知道该怎么做)。但当出现消息、警报或低电量中断时,我需要中断音频。另外,我需要在活动结束时恢复音频。
那么我如何区分这些不同的中断。
I can detect that the iPhone went to sleep and came back from sleep, by using the applicationWillResignActive and applicationDidBecomeActive. But how do I find out what kind of interrupt it was. I am making an audio player application, and need to keep the audio playing when the iPhone goes to sleep (which I know how to do). But I need to interrupt the audio when a message, alarm or low battery interrupt occurs. Also I need to resume the audio when the event is over.
So how do I differentiate between these different interrupts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的应用程序可能无法获取该信息,但可以尝试以下一些操作。
在
applicationWillResignActive:
中,检查NSNotification的对象和userInfo属性,看看是否有任何提示。注册以接收发布到默认通知中心的所有通知:
发布任何内容时都会调用您的方法。记录通知对象和 userInfo 字典,也许您会看到发布的有用通知。如果您找到了,您可以为此进行注册。
上述所有方法都将依赖于未记录的行为来工作,并且可能会导致您的提交被 App Store 拒绝。它们都不涉及私有方法调用,尽管您可能会认为观察未记录的通知名称也算作私有 API。最终,苹果的意见是唯一重要的。
就我个人而言,我会尝试一下,确保在系统发生变化时代码能够正常失败。
That information is probably not available to your app, but here's some things to try.
In
applicationWillResignActive:
, check the NSNotification's object and userInfo properties to see if there are any hints there.Register to receive all notifications posted to the default notification center:
Your method will be called when anything is posted. Log the notification object and userInfo dictionary and maybe you will see a useful notification being posted. If you find one, you can register just for that.
All of the above methods would be relying on undocumented behavior to work, and could possibly get your submission rejected from the App Store. None of them involve private method calls, though you could argue that observing an undocumented notification name counts as private API. In the end, Apple's opinion is the only one that will matter.
Personally, I'd try it, making sure the code fails gracefully if and when the system changes.
使用音频会话?
Use an audio session?