AVPlayer 在后台流式传输音频

发布于 2024-10-20 00:16:11 字数 893 浏览 3 评论 0原文

我在后台模式下播放 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 技术交流群。

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

发布评论

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

评论(1

初与友歌 2024-10-27 00:16:11

顺便说一句,问题出在模拟器上。在 iOS 设备上运行良好

By the way, the problem was just with the simulator. Works fine on the iOS devices

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