在后台运行时有响应的 iPhone 应用程序

发布于 2024-12-05 03:21:18 字数 744 浏览 0 评论 0原文

我知道您可以在应用程序中播放音乐,它会在后台运行,但看起来它不起作用。当我的应用程序在 iPhone 上启动时,我有以下代码:

pathToAudio = [[NSBundle mainBundle] pathForResource:@"silence" ofType:@"mp3"];
audioURL = [NSURL fileURLWithPath:pathToAudio];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:NULL];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];

结果是,当我将应用程序发送到后台时,它会播放音乐,但其他活动不会运行。例如,在我的应用程序中,我正在监听套接字,尽管音乐在后台播放,但我的应用程序不响应传入消息(它通常在应用程序位于前台时响应)。我做错了什么?我读过,当应用程序在后台播放音乐时,您可以执行其他活动,但它看起来不起作用...

编辑:我有越狱的 iPhone,所以欢迎任何非常规方法。我知道如果我在后台播放无声音频,Apple 会拒绝我的应用程序,而我并不关心它。我只是想知道如何让应用程序在播放音乐或以任何其他方式时在后台运行......也许我需要有 iPhone 4?我有 iPhone 3G。

I know that you can for example play music in you app and it would be running in background but it looks like it's not working. I've this code when my application starts on iPhone:

pathToAudio = [[NSBundle mainBundle] pathForResource:@"silence" ofType:@"mp3"];
audioURL = [NSURL fileURLWithPath:pathToAudio];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:NULL];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];

Result is that it plays music when I send app to background but other activities doesnt run. For example in my app I'm listening to socket and although music plays in background my app doesnt respond to incomming messages (it normally responds when app is in foreground). What do I do wrong? I've read that you can do other activities when playing music when app is in background but it looks like it doesn't work...

Edit: I have jailbroken iPhone so any unconventional methods are welcome. I know that Apple would reject my app if I was playing silent audio in background and I dont care about it. I just want to know how to make app running in background when playing music or in any other way... Maybe I need to have iPhone 4? I have iPhone 3G.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攀登最高峰 2024-12-12 03:21:18

没有什么,只能在后台运行音频(除了一些特殊情况,如 geoloc notifs 和 VoIP),而不是其他东西。

Nothing, it's only possible to run audio in background (aside with some special cases like geoloc notifs & VoIP), not other stuff.

仙女山的月亮 2024-12-12 03:21:18

您需要为 info.plist 文件提供规范,例如 UIBackgroundModes。

You'll need to provide a specification for the info.plist file like the UIBackgroundModes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文