iOS background audio works on emulator, not on device


#1

Curious question… I’m using ng-cordova media and want to allow background audio on the iOS build. When I run it in Xcode or the emulator, background audio does work when I either lock the emulator or hit the home button. This is the behavior I would expect. However, when I load the app on an actually iPhone, audio stops on lock or home. What am I missing? Is this a bug in CLI? Not updating a plist setting or something? Thanks.


#2

This worked for me:

If you need background audio, you will need to add the supported modes in your -info.plist

<key>UIBackgroundModes</key>
    <array>
        <string>audio</string>
    </array>

Also important to note, since iOS6, you must explicitly set the AVAudioSessionCategory.

First, import AVFoundation into your AppDelegate.m

#import <AVFoundation/AVFoundation.h>

Then add the following to application:(UIApplication)application didFinishLaunchingWithOptions:(NSDictionary)launchOptions**

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL ok;
NSError *setCategoryError = nil;
ok = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];

Taken from: https://github.com/devgeeks/ExampleHTML5AudioStreaming