AVPlayer 在后台流式传输音频
我在后台模式下播放 AVPlayer 时遇到问题,就像这里和网络上其他地方的很多人一样。我已经完成了我认为应该有效的操作,但仍然不起作用...我认为我的问题可能是我设置和使用 AudioSession 和 AVPlayer 的位置。
1)“音频”键位于我的 Info.plist 的 UIBackgroundModes 中
2)在 AppDelegate 中像这样设置 AudioSession (在 didFinishLaunchingWithOptions 中初始化):
AVAudioSession *audio = [[AVAudioSession alloc]init];
[audio setCategory:AVAudioSessionCategoryPlayback error:nil];
[audio setActive:YES error:nil];
3)我使用也在 AppDelegate 中实现的 AVPlayer (不是 AVAudioPlayer)。 (在 AudioSession 之后的 didFinishLaunchingWithOptions 中初始化),就在 AudioSession 之后,
// Load the array with the sample file
NSString *urlAddress = @"http://mystreamadress.mp3";
//Create a URL object.
self.urlStream = [NSURL URLWithString:urlAddress];
self.player = [AVPlayer playerWithURL:urlStream];
//Starts playback
[player play];
每次应用程序进入后台时(当我按“主页”按钮时),音频都会暂停。
I have a problem with the AVPlayer playing in background mode, like a lot of people here and everywhere on the web. I've done what I think is supposed to work, but is still doesn't... I think my problem might be where I set and use my AudioSession and AVPlayer.
1) The "audio" key is in UIBackgroundModes of my Info.plist
2) AudioSession set like this in AppDelegate (initialised in didFinishLaunchingWithOptions):
AVAudioSession *audio = [[AVAudioSession alloc]init];
[audio setCategory:AVAudioSessionCategoryPlayback error:nil];
[audio setActive:YES error:nil];
3) I use an AVPlayer (not AVAudioPlayer) also implemented in AppDelegate. (initialised in didFinishLaunchingWithOptions, after the AudioSession), right after the AudioSession
// Load the array with the sample file
NSString *urlAddress = @"http://mystreamadress.mp3";
//Create a URL object.
self.urlStream = [NSURL URLWithString:urlAddress];
self.player = [AVPlayer playerWithURL:urlStream];
//Starts playback
[player play];
And still, the audio is suspended everytime the app goes in background (when I press the "Home" button).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
顺便说一句,问题出在模拟器上。在 iOS 设备上运行良好
By the way, the problem was just with the simulator. Works fine on the iOS devices