1

I need audio running in the background for my application. In versions before iOS 6 this worked perfectly by adding the

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

code to the appropriate .plist file, however this no longer works when deploying my phonegap project to iOS 6. How can I fix this in iOS 6?

Ayame__
  • 980
  • 2
  • 9
  • 21
  • I have found a similar topic here: http://stackoverflow.com/questions/11616001/uiwebview-html5-audio-pauses-in-ios-6-when-app-enters-background#comment17805618_12414719 but I have no experience with XCode so I'm not sure how to make this work. I pasted the code in the MainController.m file under // custom initialisation, but then my app hangs when launching. Help would be greatly appreciated! – Ayame__ Oct 30 '12 at 16:23
  • 1
    I just want to point out that this also effects how audio plays when the ringer is off. Normally, HTML5 audio element in a UIWebView will only play if the ringer is on. Following the steps described in the answer will allow audio to play when the ringer is off. – bjudson Jan 24 '13 at 20:17

1 Answers1

2

In the MainController.h file change the following:

#import "MainViewController.h"
#import <AVFoundation/AVFoundation.h>

@implementation MainViewController

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

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

    }
    return self;
}

I'm not sure if the following is necessary but I cleaned my project (CMD+ALT+K), recompiled the PhoneGap / Cordova Lib (change target and let it run to simulator) and (after changing the target again) compiled to the application once more and now it works!

Beware: it only works on Device, not on the simulator

Ayame__
  • 980
  • 2
  • 9
  • 21