iOS背景音频流(m3u)不';在设备上不工作(但在模拟器中不工作)
我有一个从url加载m3u的UIWebView:iOS背景音频流(m3u)不';在设备上不工作(但在模拟器中不工作),ios,audio,background,m3u,Ios,Audio,Background,M3u,我有一个从url加载m3u的UIWebView: NSString *url = @"http://141.217.20.38:8000/live.m3u"; [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]]; 其次是: [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; UIBa
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(带有开发配置文件)将其安装到设备(iPhone5)上时,音频不会在后台播放
我不确定我错过了什么,但如果有任何帮助,我将不胜感激。
beginBackgroundTaskWithExpirationHandler:
用于执行一项冗长的任务,而不是让你的应用程序在后台运行
你需要在
info.plist
中注册你的应用程序背景模式,将背景模式(UIBackgroundModes
)设置为audio
。如中所述,似乎还需要创建AVSession。我添加了以下代码,现在它在设备上的工作方式与在模拟器中的相同
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL ok;
NSError *setCategoryError = nil;
ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
error:&setCategoryError];
if (!ok) {
NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
}
我让应用程序在所需的背景模式下使用AirPlay播放音频或流式播放音频/视频。没有AirPlay,您可以选择
音频
,位置
,voip
,获取
,远程通知
,报摊内容
,外部配件
,bluetooth central
和bluetooth peripheral
。我假设这是在后台模式下选中“Audio and Airplay”复选框后由Xcode设置的:我将音频添加到列表中,但仍然得到相同的行为:后台音频在模拟器中工作,但不在设备上。info.plist
的实际内容是ui背景模式音频远程通知