0

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.

cplater
  • 33
  • 9

2 Answers2

1

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);
}
cplater
  • 33
  • 9
0

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

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • I have App plays audio or streams audio/video using AirPlay set in Required Background Modes. – cplater May 01 '14 at 14:49
  • There is no [`UIBackgroundModes`](https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW22) AirPlay, you options are `audio`, `location`, `voip`, `fetch`, `remote-notification`, `newsstand-content`, `external-accessory`, `bluetooth-central` and `bluetooth-peripheral`. – rckoenes May 01 '14 at 14:53
  • I'm assuming this was set by Xcode after checking the "Audio and Airplay" Checkbox under Background Modes: [Screenshot](https://www.dropbox.com/s/hax1ucr28wmkqt5/Screenshot%202014-05-01%2010.57.01.png) – cplater May 01 '14 at 15:10
  • I added Audio to the list, and I still get the same behavior: Background audio works in the simulator, but not on a device. – cplater May 01 '14 at 15:12
  • The actual content of the `info.plist` is `UIBackgroundModes audio remote-notification ` – cplater May 01 '14 at 15:21