gpt4 book ai didi

iphone - 设备锁定时响应事件

转载 作者:可可西里 更新时间:2023-11-01 04:44:41 26 4
gpt4 key购买 nike

我正在研究 radio 闹钟,但遇到了一些问题。我正在为警报使用本地通知,因此如果应用程序未运行,它会有一个温和的回退。

我很清楚设备的局限性,我知道当设备进入后台时我能做什么和不能做什么。

但我的问题是:当我锁定设备时,我看到其他应用程序启动了音频流。这怎么可能?这可能在执行时间范围内吗?

实现这个的最佳方法是什么?当设备锁定时,我可以通过任何方式激活流式传输 session 吗?

编辑

澄清一下:我知道如何在后台播放音频。但问题是在触发本地通知或其他事件时触发音频播放。

一个似乎可以做到这一点的应用程序是 Radio Alarm Clock .我已经很久没有尝试过了。但它似乎是这样做的。该应用程序的视频演示:http://www.youtube.com/watch?v=KJQiFOcdBWk

最佳答案

你是否已经声明了你的后台任务?

Support for some types of background execution must be declared in advance by the app that uses them. An app declares support for a service using its Info.plist file. Add the UIBackgroundModes key to your Info.plist file and set its value to an array containing one or more of the following strings:

audio — The app plays audible content to the user while in the background. (This content includes streaming audio or video content using AirPlay.)

iOS App Programming Guide - Implementing Long Running Background Tasks

您可以通过单击主项目图标来添加它,然后在“信息”选项卡下,您可以将“必需的背景模式”添加到“自定义 iOS 目标属性”部分。 “App Plays Audio”将是三个默认值之一。

新答案的大修改:

如果其他一切都已准备就绪,您可以使用 UIApplication 方法让您的应用程序在后台运行

- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler

详情在这里:UIApplication Class Reference

这里有一个例子:Hour 21: Building Background-Aware Applications

这允许您运行一个触发音乐播放器的 NSTimer 实例。这种方法与 UILocalNotifications 之间的区别在于,这种方法永远不会让应用程序完全进入后台模式,音乐播放器一直存在,这颠覆了从后台创建它的需要,这看起来是不可能的。

您可以设置多长时间的计时器可能有限制,过去 14 分钟我还没有测试过。

关于iphone - 设备锁定时响应事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com