ios - iOS 上后台换台

标签 ios audio-streaming

我想知道我的 iPhone 上是否有广播流媒体应用程序,我是否可以让该应用程序在后台运行时自动切换到新电台? 到目前为止我听说苹果不允许在后台执行任何操作。仅允许某些处于特定状态的事件运行,直到这些状态结束。

非常感谢

最佳答案

根据iOS Developer Documentation这是可能的。

In iOS 4 and later, multitasking allows apps to continue running in the background even after the user switches to another app while still preserving battery life as much as possible. Most apps are moved to the suspended state shortly after entering the background. Only apps that provide important services to the user are allowed to continue running for any amount of time.

As much as possible, you are encouraged to avoid executing in the background and let your app be suspended. If you find you need to perform background tasks, here are some guidelines for when that is appropriate:

You need to implement at least one of several specific user services.
You need to perform a single finite-length task.
You need to use notifications to alert the user to some relevant piece of information when your app is not running.
The system keeps suspended apps in memory for as long as possible, removing them only when the amount of free memory gets low. Remaining in memory means that subsequent launches of your app are much faster. At the same time, being suspended means your app does not drain the device’s battery as fast.

然后...

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

Apps that play audible content to the user while in the background, such as a music player app Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Newsstand apps that need to download and process new content
Apps that receive regular updates from external accessories
Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

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.) location—The app keeps users informed of their location, even while it is running in the background.
voip—The app provides the ability for the user to make phone calls using an Internet connection.
newsstand-content—The app is a Newsstand app that downloads and processes magazine or newspaper content in the background.
external-accessory—The app works with a hardware accessory that needs to deliver updates on a regular schedule through the External Accessory framework.
bluetooth-central—The app works with a Bluetooth accessory that needs to deliver updates on a regular schedule through the CoreBluetooth framework.
Each of the preceding values lets the system know that your app should be woken up at appropriate times to respond to relevant events. For example, an app that begins playing music and then moves to the background still needs execution time to fill the audio output buffers. Including the audio key tells the system frameworks that they should continue playing and make the necessary callbacks to the app at appropriate intervals. If the app does not include this key, any audio being played by the app stops when the app moves to the background.

An app that plays audio continuously (even while the app is running in the background) can register as a background audio app by including the UIBackgroundModes key (with the value audio) in its Info.plist file. Apps that include this key must play audible content to the user while in the background.

Typical examples of background audio apps include:

Music player apps
Apps that support audio or video playback over AirPlay
VoIP apps
When the UIBackgroundModes key contains the audio value, the system’s media frameworks automatically prevent the corresponding app from being suspended when it moves to the background. As long as it is playing audio or video content, the app continues to run in the background. However, if the app stops playing the audio or video, the system suspends it.

You can use any of the system audio frameworks to initiate the playback of background audio, and the process for using those frameworks is unchanged. (For video playback over AirPlay, you must use the Media Player framework to present your video.) Because your app is not suspended while playing media files, callbacks operate normally while your app is in the background. In your callbacks, though, you should do only the work necessary to provide data for playback. For example, a streaming audio app would need to download the music stream data from its server and push the current audio samples out for playback. You should not perform any extraneous tasks that are unrelated to playback.

Because more than one app may support audio, the system limits which apps can play audio at any given time. The foreground app always has permission to play audio. In addition, one or more background apps may also be allowed to play some audio content depending on the configuration of their audio session objects. You should always configure your app’s audio session object appropriately and work carefully with the system frameworks to handle interruptions and other types of audio-related notifications. For information on how to configure audio session objects for background execution, see Audio Session Programming Guide.

要阅读完整文档,请转到答案开头的链接,然后转到标题后台执行和多任务。记得检查安装该应用的设备是否具有多任务功能;只有 iOS4+ 设备才有。

关于ios - iOS 上后台换台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10890258/

相关文章:

iphone - 我收到 sqlite3_prepare_v2 错误

audio - 录制带有大量静音的流式音频

ios - 如何以编程方式更改detailTextLabel高度

ios - 当我的应用程序处于后台时在 iOS 上捕获硬件按钮

ios - NSUserDefaults setValue : deletes my plist. 发生了什么?

java - Google App Engine 中使用 Java Servlet 进行音频流传输

java - 如何从字节数组计算音频剪辑的持续时间?

ios - Swift Google Place Autocomplete 无法与 Google map 配合使用

java - "Endless"来自套接字的 AudioInputStream

c++ - PortAudio 实时串流和处理