Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML 5 audio MP3 file not playing back when iPhone locked using PhoneGap 2.1.0 and iOS 6

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?

like image 277
user1726961 Avatar asked Oct 07 '12 16:10

user1726961


1 Answers

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.

like image 77
Devgeeks Avatar answered Sep 28 '22 13:09

Devgeeks