Phonegap - play local sound when locking your iOS 6 phone

I am creating an application using PhoneGap that should play local .mp3 files even if the phone is locked / standby. The sound player is built in HTML5 and works great, but the music stops when I close the application or turn off the phone.

I tried following the answer provided in this UIWebView link : HTML5 audio files in iOS 6 when the application enters the wallpaper

But no luck ...

I made the import code at the top with other import functions. And I also included the scope of AVFoundation in my goal.

This is what the code looks like in AppDelegate.m

/** * This is main kick off after the app inits, the views and Settings are setup here. (preferred -iOS4 and up) */ - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { /* THE GOOD STUFF */ AVAudioSession *audioSession = [AVAudioSession sharedInstance]; BOOL ok; NSError *setCategoryError = nil; ok = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError]; if (!ok) { NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError); } 

I tried in the simulator and on the iPhone 5.

Can anybody help?

+4
source share
1 answer

First, you must set your audio category in your app_name-info.plist App Audio Category

Then use the Phonegap API Media Player http://docs.phonegap.com/en/2.7.0/cordova_media_media.md.html#media.play

 function playAudio(url) { // Play the audio file at url var my_media = new Media(url, // success callback function() { console.log("playAudio():Audio Success"); }, // error callback function(err) { console.log("playAudio():Audio Error: "+err); }); // Play audio my_media.play({ playAudioWhenScreenIsLocked : true }); 

Note that the default value of playAudioWhenScreenIsLocked is true.

Also note that in the simulator, Audio will never run in the background, so you will need to test the device.

Mark as "Answer" if this helped?

+4
source

Source: https://habr.com/ru/post/1482349/


All Articles