如何使用 AVAudioPlayer 在后台播放音乐?
我目前使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模拟器中显然存在一个错误 - 它在那里不起作用,但它在设备上运行良好。
(编辑:“不起作用”是指当应用程序进入后台时声音停止,但播放器的状态仍保持“正在播放”,并且似乎继续“播放”,尽管您听不到)。
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).