iOS后台音频流(m3u)在设备上不起作用(但在模拟器中起作用)



我有一个UIWebView,它从url:加载m3u

NSString *url = @"http://141.217.20.38:8000/live.m3u";
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];

然后是:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

这在模拟器上有效,但当我通过Xcode(带有开发配置文件(将其安装在设备(iPhone 5(上时,音频不会在后台播放。

我不确定我错过了什么,但如果有任何帮助,我们将不胜感激。

因此,看起来还需要创建一个AVSession。我添加了以下代码,现在它在设备上的工作方式与模拟器中的相同

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL ok;
NSError *setCategoryError = nil;
ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
                         error:&setCategoryError];
if (!ok) {
    NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
}

beginBackgroundTaskWithExpirationHandler:用于执行冗长的任务,而不是让应用程序在后台运行。

您需要在info.plist中注册应用程序后台模式,将后台模式(UIBackgroundModes(设置为audio。如应用程序状态和多任务中所述

相关内容

最新更新