7

I have a PhoneGap app which I wrote under PhoneGap 1.8.1 and it worked great under iOS 5.1.

I needed to change the app so downloaded the latest Xcode 4.5 and the iOS 6 SDK. But when I build the app and deploy it to my iPhone 4s, the HTML 5 audio will not play back when the phone is locked.

I tried upgrading the app to use PhoneGap 2.1.0 but when the phone is locked the app will still not play the audio. (Which is an mp3 file stream from my server.)

It all works fine in the simulator but does not work when on my iPhone 4s running iOS 6.

I have Required background modes set to App plays audio and Application does not run in background = Yes. Which used to work under iOS 5.1 but does not under iOS 6. I have also tried to modify the sound.mfile which some have recommended. Even though I did not need to do this under iOS 5.1

I have tried a bunch of examples I found on the internet but none seem to work in iOS 6. And allow HTML 5 audio to stream in with the phone locked.

What do I need to do to get HTML 5 audio to play when an iPhone is locked in iOS 6 under PhoneGap 1.8.1 or 2.1?

user2428118
  • 7,935
  • 4
  • 45
  • 72
user1726961
  • 71
  • 1
  • 2
  • Did you try this? http://stackoverflow.com/questions/11616001/uiwebview-html5-audio-pauses-in-ios-6-when-app-enters-background/12414719#12414719 – German Latorre Jun 04 '13 at 10:24

1 Answers1

4

Doesn't Application does not run in background = Yes force the app to terminate instead of running in the background?

Isn't that the opposite of what you want? Perhaps try removing that key and just leaving the UIBackgroundModes set to audio?

EDIT:

It appears that since iOS 6 your application MUST set the AVAudioSessionCategory to AVAudioSessionCategoryPlayback to be able to play audio in the background. PhoneGap/Cordova does this for when you use the PhoneGap/Cordova Media API, but when using HTML5 audio it never gets set.

I tested using my ExampleHTML5AudioStreaming project and adding the UIBackgroundModes -> audio only but explicitly setting the AVAudioSessionCategory to AVAudioSessionCategoryPlayback in the AppDelegate.m file (under ProjectName/Classes).

I first imported AVFoundation in AppDelegate:

#import <AVFoundation/AVFoundation.h>

I then added the following to application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

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

As per this answer: https://stackoverflow.com/a/12414719/878602

Audio then played in the background as expected.

It looks like there is no bug lodged for Cordova for this. I will see if I can lodge one.

Community
  • 1
  • 1
Devgeeks
  • 5,659
  • 4
  • 28
  • 35
  • You would think so. But I tried not having it and it did not work. With this weird setup the app stops but the audio continue in iOS 5 but now in iOS 6 it does not work. I tried a few example projects people posted to github for how to do HTML 5 audio in the background but ow of them work in iOS 6 – user1726961 Oct 08 '12 at 13:08
  • Just for fun I tried again and removed the Application does not run in background again. The audio still fades out when you press the lock button. – user1726961 Oct 08 '12 at 13:24
  • Plese see this for iOS 6 and upper.. http://stackoverflow.com/questions/3413258/give-some-screenshots-to-create-uibackgroundmodes-key-in-info-plist-for-ios4 – Hussain KMR Behestee Jan 09 '14 at 08:08
  • Thanks, I managed to make my HTML5 audio element to work in the background/locked mode using this info. – Pirkka Esko Feb 26 '14 at 07:48