简体   繁体   中英

Phonegap app works in IOS background

I have a music Phonegap app. When playing a music, I click the Home button, then the music stops. Is there anyway that even if the home button is clicked, the Phonegap will still run normally?

Thank you for your suggestion in advance!!

I am using Phonegap build and my config.xml is as below.

<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets"
    xmlns:gap = "http://phonegap.com/ns/1.0"
    id        = "com.myMusic.myMusic"
    version   = "1.0.15">

  <name>MyMusic</name>

  <description>
    MyMusic
  </description>

  <gap:platform name="ios" />
  <gap:platform name="android" />

  <preference name="permissions"      value="none"/>

  <preference name="phonegap-version" value="3.1.0" />
  <gap:plugin name="de.appplant.cordova.plugin.background-mode" version="0.5.0" />
  <gap:plugin name="org.apache.cordova.camera" version="0.2.9" />
    <gap:plugin name="org.apache.cordova.media-capture" version="0.2.8" />
    <gap:plugin name="org.apache.cordova.battery-status" version="0.2.7" />
    <gap:plugin name="org.apache.cordova.device" version="0.2.8" />
    <gap:plugin name="org.apache.cordova.network-information" version="0.2.7" />
    <gap:plugin name="org.apache.cordova.dialogs" version="0.2.6"/>
    <gap:plugin name="org.apache.cordova.splashscreen" version="0.2.7" />
    <gap:plugin name="org.apache.cordova.geolocation" version="0.3.7" />
    <gap:plugin name="org.apache.cordova.file" version="1.0.1" />
    <gap:plugin name="org.apache.cordova.media" version="0.2.8" />
    <gap:plugin name="org.apache.cordova.inappbrowser" version="0.2.4" />

  <preference name="orientation"      value="portrait" />
  <preference name="target-device"    value="universal" />
  <preference name="fullscreen"       value="false" />
  <preference name="webviewbounce"    value="false" />
  <preference name="DisallowOverscroll" value="true" />

  <access origin="*" />


</widget>

Doing a very quick and brief search, you'll find out that this question has already been asked so so, so, so many times already and there are good answers to it.

However, this should solve your problem:

In your info.plist file, add a new key of Required background modes . That key will have an array of options. In Item 0 of that array set the value to App plays audio .

Then, modify the ~/PhoneGapLib/Classes/Sound.m file (you can use Finder to search for Sound.m ). Look for the line that contains if ([resourceURL isFileURL]) { . Just below that line, inside the if statement, add the following:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

So, that section of code will look like the following:

if ([resourceURL isFileURL]) {
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    audioFile.player = [[ AVAudioPlayer alloc ] initWithContentsOfURL:resourceURL error:&error];
} else {
    NSData* data = [NSData dataWithContentsOfURL:resourceURL];
    audioFile.player = [[ AVAudioPlayer alloc ] initWithData:data error:&error];
}

这个插件似乎解决了这个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM