如何使用 AVAudioPlayer 在后台播放音乐?

发布于 2024-09-13 03:04:07 字数 231 浏览 1 评论 0原文

我目前使用 AVAudioPlayer 在前台播放音乐,但是当我按下主页按钮时,我的应用程序将转到后台并且音乐停止。我将 AVAudioSession 的类别设置为 AVAudioSessionCategoryPlayback 并将 active 设置为 YES。此外,我将 audio 键添加到 Info.plist 中的 UIBackgroundModes 中。

我还需要做什么才能启用背景音乐播放吗?

I currently use AVAudioPlayer to play music while in the foreground, but when I press the home button my application goes to the background and the music stops. I set the category for my AVAudioSession to AVAudioSessionCategoryPlayback and set active to YES. Additionally, I added the audio key to UIBackgroundModes in my Info.plist.

Is there anything else I need to do to enable background music playback?

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

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

发布评论

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

评论(1

高冷爸爸 2024-09-20 03:04:07

模拟器中显然存在一个错误 - 它在那里不起作用,但它在设备上运行良好。

(编辑:“不起作用”是指当应用程序进入后台时声音停止,但播放器的状态仍保持“正在播放”,并且似乎继续“播放”,尽管您听不到)。

There's apparently a bug in the simulator - it doesn't work there, while it works fine on the device.

(Edit: and by "doesn't work" I mean the sound stops when the app enters background, yet the player's status remains "playing" and it seems to keep "playing", although you can't hear it).

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