xcode 如何将 iOS 应用程序置于后台模式,并设置音频选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6796588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:32:31  来源:igfitidea点击:

How do I put iOS application in background mode, with audio option set?

iphoneiosxcodebackgroundaccelerometer

提问by Ricky Robinson

So my application for iphone4 reads data from the accelerometer and sends it to another application via tcp sockets. I need my app to work in background mode, so what I did was:

所以我的 iphone4 应用程序从加速度计读取数据并通过 tcp 套接字将其发送到另一个应用程序。我需要我的应用程序在后台模式下工作,所以我所做的是:

  • I put an mp3 file in the application's Documents folder
  • I used AVAudioPlayerlibrary to play the file in a loop. It works.
  • I edited Info.plistand added option "required background modes" with "audio" on.
  • 我在应用程序的Documents 文件夹中放置了一个 mp3 文件
  • 我使用AVAudioPlayer库循环播放文件。有用。
  • 我编辑了Info.plist并添加了选项“必需的背景模式”并打开“音频”。

Still, the scheduler suspends the application whenever I press the iphone's home button. Is there anything I missed?

尽管如此,每当我按下 iPhone 的主页按钮时,调度程序都会暂停应用程序。有什么我错过的吗?

I read apple's documentation, but I didn't find a solution. A few thoughts on this:

我阅读了苹果的文档,但没有找到解决方案。对此有几点想法:

  • do I have to edit appDelegate.m?
  • is it because I use AVAudioPlayer instead of the iPod?
  • is it because I play an audio file from the application documents folder?
  • I read about one person changing iOS Development Target from 4.0 to 3.2.1, but that didn't work for me.
  • 我必须编辑 appDelegate.m 吗?
  • 是因为我使用 AVAudioPlayer 而不是iPod吗?
  • 是因为我播放了应用程序文档文件夹中的音频文件吗?
  • 我读到有人将 iOS 开发目标从 4.0 更改为 3.2.1,但这对我不起作用。

And finally, say I get this to work, would the application still be getting data from the accelerometer?

最后,假设我让它工作,应用程序是否仍会从加速度计获取数据?

On a side note, I don't want to submit the application to the App Store.

附带说明一下,我不想将应用程序提交到 App Store。

回答by Anton

No, you will not receive accelerometer notifications in background mode. As far as I know, it is not possible. Check Executing Code in Background.

不,您不会在后台模式下收到加速度计通知。据我所知,这是不可能的。检查在后台执行代码

If you read the docs carefully, you will know that the whole background code model is based on responding to specific events (locationand voipmodes).

如果你仔细阅读文档,你就会知道整个后台代码模型是基于对特定事件(locationvoip模式)的响应。

As for the audiomode here is an extract from Apple:

至于audio这里的模式是从Apple摘录的:

Your application should limit itself to doing only the work necessary to provide data for playback while in the background. For example, a streaming audio application would download any new data from its server and push the current audio samples out for playback. You should not perform any extraneous tasks that are unrelated to playing the content.

您的应用程序应将自身限制为仅提供在后台播放所需的数据。例如,流式音频应用程序会从其服务器下载任何新数据并将当前音频样本推送出去进行播放。您不应执行任何与播放内容无关的无关任务。

回答by toppest

Not sure whether you have solved your issue or not since this question was posted more than one year ago. Also, not sure whether playing audio is a must in your app or not. If both answers are no, my recent investigation may help a bit.

不确定您是否已经解决了您的问题,因为这个问题是在一年多前发布的。另外,不确定在您的应用中是否必须播放音频。如果两个答案都不是,我最近的调查可能会有所帮助。

Here are how I get my app getting accelerometer data at the background 1. Follow this tutorial http://mobile.tutsplus.com/tutorials/iphone/ios-multitasking-background-location/to get the background location working. 2. Follow this tutorial http://jonathanhui.com/ios-motionto get the accelerometer working.

以下是我如何让我的应用程序在后台获取加速度计数据 1. 按照本教程http://mobile.tutsplus.com/tutorials/iphone/ios-multitasking-background-location/获取后台位置工作。2. 按照本教程http://jonathanhui.com/ios-motion使加速度计工作。

Then you can get an app collecting accelerometer data at the background. Hope this helps.

然后你可以得到一个在后台收集加速度计数据的应用程序。希望这可以帮助。