문제

I have a UIWebView that loads a m3u from a url:

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

followed by:

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

This works on the simulator, but when I install it on a device (iPhone 5) via Xcode (with a development provisioning profile) the audio won't play in the background.

I'm not sure what I've missed, but any help would be greatly appreciated.

도움이 되었습니까?

해결책

So, it looks like an AVSession also needs to be created. I added the following code, and it now works on the device the same as in the simulator

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

다른 팁

beginBackgroundTaskWithExpirationHandler: is for executing a lengthy task, not for keeping you app running in the background.

You need to register your apps background mode in info.plist set the background mode(UIBackgroundModes) to audio. As descripted in the App States and Multitasking

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top