我有一个从网址加载m3u的UIWebView:
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)上安装时,音频不会在后台播放。
我不确定我错过了什么,但我们将非常感谢任何帮助。
答案 0 :(得分:1)
因此,看起来还需要创建AVSession。我添加了以下代码,它现在可以在设备上与模拟器中的相同
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL ok;
NSError *setCategoryError = nil;
ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
error:&setCategoryError];
if (!ok) {
NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
}
答案 1 :(得分:0)
beginBackgroundTaskWithExpirationHandler:
用于执行冗长的任务,而非用于让您的应用在后台运行。
您需要在info.plist
中注册应用后台模式,将后台模式(UIBackgroundModes
)设置为audio
。如App States and Multitasking