AVAudioSession后台模式问题
我已将 AVAudioSession 设置为在后台播放音乐,
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (setCategoryError) {
}
NSError *activationError = nil;
[audioSession setActive:YES error:&activationError];
if (activationError) {
}
还在我的 plist 文件中添加了“必需的背景模式”。
当我播放远程文件并且 iPhone 进入后台时,音频会像怀疑的那样继续播放。但是,当我在应用程序 (MPMediaItem URL) 中播放 iPod 中的歌曲时,它不会继续在后台播放。
我缺少什么?
I have set up my AVAudioSession to play music in the background
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (setCategoryError) {
}
NSError *activationError = nil;
[audioSession setActive:YES error:&activationError];
if (activationError) {
}
Also added 'Required background modes' in my plist file.
When I play a remote file and the iPhone enters the background the audio keeps playing as suspected. But when I play a song from the iPod inside my app (MPMediaItem URL) it won't continue to play in the background.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你没有遗漏任何东西。当用户开始从 iPod 播放音乐时,它将阻止您的应用程序播放任何其他音频。
如果您有其他意思,请留言,我将更新此答案。
编辑:您应该通过在
MPMusicPlayerController
的相关实例上设置队列来启动 iPod 音乐,然后向其发送-play
消息。如果您将MPMediaItem
对象提供的 URL 与AVAudioPlayer
实例或类似实例一起使用,它将以不同的方式路由,并且可能无法继续在后台播放。You're not missing anything. When the user starts playing music from iPod, it will stop your app from playing any further audio.
If you mean something else, please leave a note and I'll update this answer.
Edit: You should start iPod music by setting the queue on the relevant instance of
MPMusicPlayerController
, and then sending it the-play
message. If you use the URL provided by theMPMediaItem
object with anAVAudioPlayer
instance or similar, it will be routed differently and may not be able to continue playing in the background.