Skip to main content
Pouradam
Inspiring
November 4, 2013
Answered

Keep playing sound after pressing home button in iOS

  • November 4, 2013
  • 2 replies
  • 3939 views

Hi there!

  I have an App that loads an MP3 from a URL and then play it (Flash cs6 - Air 3.9)

In Android (when I publish it as APK) , when user press Power, or Minimize the App, the sound keeps playing (as I want it)

But same code in iOS, when I press Home or Power, the sound get  "PAUSED", and keep playing only after I re-activate my App.

I tried to solve it with this line of code:

NativeApplication.nativeApplication.executeInBackground = true;

But no success.

Would anyone please help me what should I do to prevent iOS from PAUSING the sound in my APP after pressing Home and/or Power ?

Thank you in advanced.

This topic has been closed for replies.
Correct answer AsterLUXman

Hi Pouradam,

I know it sucks.

On iOS, the only way to have a sound be played while the app is minimized, is if the app is actually running in the background ( obviously ).  To do this, in the app manifest, you need to set UIBackgroundModes = audio, like so:


<InfoAdditions>

        <![CDATA[

            <key>UIBackgroundModes</key>

            <array>

                <string>audio</string>

            </array>

        ]]>

    </InfoAdditions>

However, ever since "batterygate" on iPhone4, Apple has been tightening its policy on apps that are needlessly running in the background.  If the sound is meant to play continuously ( ex: music player ), then they will *probably* be ok with it.  But if the sound is intermittent ( even if it is the alarm sound of an alarm-clock ), they will probably disallow it.

So, if you want your app to play intermittent sounds while in the background, your only chance is to set up notifications, and associate a sound to the notifications if you can.

However, should you try the local notification route, using Juan Carlos Pazmino's ANE, be advised that the sound feature is broken at the moment, at least on iOS 5.1.1.  Also, back when it was working, I wasn't able to choose the sound via the app:  all I got was the standard "ding" sound.  Maybe push notifications are more robust.

Cheers.

2 replies

AsterLUXmanCorrect answer
Inspiring
November 12, 2013

Hi Pouradam,

I know it sucks.

On iOS, the only way to have a sound be played while the app is minimized, is if the app is actually running in the background ( obviously ).  To do this, in the app manifest, you need to set UIBackgroundModes = audio, like so:


<InfoAdditions>

        <![CDATA[

            <key>UIBackgroundModes</key>

            <array>

                <string>audio</string>

            </array>

        ]]>

    </InfoAdditions>

However, ever since "batterygate" on iPhone4, Apple has been tightening its policy on apps that are needlessly running in the background.  If the sound is meant to play continuously ( ex: music player ), then they will *probably* be ok with it.  But if the sound is intermittent ( even if it is the alarm sound of an alarm-clock ), they will probably disallow it.

So, if you want your app to play intermittent sounds while in the background, your only chance is to set up notifications, and associate a sound to the notifications if you can.

However, should you try the local notification route, using Juan Carlos Pazmino's ANE, be advised that the sound feature is broken at the moment, at least on iOS 5.1.1.  Also, back when it was working, I wasn't able to choose the sound via the app:  all I got was the standard "ding" sound.  Maybe push notifications are more robust.

Cheers.

Pouradam
PouradamAuthor
Inspiring
November 12, 2013

Wow!! Hi AsterLUXman and thank you so much for your complete reply!

I was nearly forgot what I wanted to do and still I have not found any solution for that.

My App is something like a Music player, so based on your note I can do that hopefully!

Now, may I ask if it is possible I do it INSIDE flash? I mean is that possible I code this inside my App or I have to change the XML as mentioned??

Thanks again - Ali

Inspiring
November 12, 2013

As far as I know, you can only enable the UIBackgroundmodes via the app manifest.

Pouradam
PouradamAuthor
Inspiring
November 4, 2013

Please correct me If I have posted this question in the wrong topic?!

kglad
Community Expert
Community Expert
November 4, 2013

no, this is the correct forum. 

i just don't think i can be helpful because (imo) that behavior is expected (and desirable in iOS) and is unexpected (and undesirable in android).