1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

How do I get the audio running in background (iOS)?

Discussion in 'Audio & Video' started by mfgage, Apr 18, 2015.

  1. mfgage

    mfgage

    Joined:
    Jun 4, 2013
    Posts:
    10
    My app needs to have the audio run in the background mode

    In Unity, I set:
    Player Settings... --> "Behavior in Background"
    to be "Custom" and also added the line:
    Application.runInBackground = true;
    in the C# initialization script.

    The audio is still stopping when either
    screen dims OR
    lock button is pressed.

    I get this in Xcode too:
    -> applicationWillResignActive()
    -> applicationDidEnterBackground()

    Please help.
     
  2. aihodge

    aihodge

    Joined:
    Nov 23, 2014
    Posts:
    101
    Try this: In the Xcode project generated by Unity, select the Unity-iPhone target, and go to Capabilities > Background Modes. Turn Background Modes on and check "Audio and Airplay".
     
  3. mfgage

    mfgage

    Joined:
    Jun 4, 2013
    Posts:
    10
    That makes total sense, but it still is NOT playing after entering sleep/lock state.

    Thanks so much for the input, alhodge. Any other ideas?

    I built this entire app based on being able to have this functionality. Now, I'm done and in a real quandary if I can not get this functioning. I know how easy this is to do in an iOS app and just assumed it would work on Unity.

    Also, in Unity I have tried setting both "Custom" & "Suspend" for the item "Behavior in Background" and it seems to have no effect on anything.
     
  4. aihodge

    aihodge

    Joined:
    Nov 23, 2014
    Posts:
    101
    You may need to configure the AVAudioSession in the Xcode project too. Try putting this in UnityAppController.mm's didFinishLaunchingWithOptions:

    Updated October 6, 2016
    Code (CSharp):
    1.  
    2.         [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlaybackerror:nil];
    3.  
    4.         [[AVAudioSession sharedInstance] setActive: YESerror: nil];
    5.  
    6.         [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    7.  
    See this page in Apple's documentation concerning background audio: https://developer.apple.com/library/ios/qa/qa1668/_index.html#//apple_ref/doc/uid/DTS40010209
     
    Last edited: Oct 7, 2016
  5. mfgage

    mfgage

    Joined:
    Jun 4, 2013
    Posts:
    10
    For some reason it's working now after the 2nd rebuild from Unity. So, your original idea DID work! (ostensibly at least).

    FWIW, I am using PlayerSettings --> "Behavior in Background" = "Custom".

    I will come back to your 2nd suggestion if needed down the line. Thanks!
     
  6. francesco-delpia

    francesco-delpia

    Joined:
    Feb 18, 2014
    Posts:
    1
    Hi @mfgage, it's been some time since the previous posts so I wanted to know if the solution worked fine for you. Also, did you use Unity's internal audio engine or a third party (FMOD, Wwise, other)? I'm particularly interested in developing a Unity app with audio coming from Wwise that should play while the app is in the background as well. Do you know if it is possible? Thanks!
     
  7. mfgage

    mfgage

    Joined:
    Jun 4, 2013
    Posts:
    10
    Hi. Yes this did work -- just as I explained. I'm uncertain as to whether the Android version functions the same as the iOS, but background audio is working on all my iOS devices. No I did not use FMOD. I solely used Unity in my project. Hope this helps.
     
  8. WzL

    WzL

    Joined:
    Jul 15, 2014
    Posts:
    1
    Hi, how did you get this to work? I have Background behavior -> Custom. Enabled background mode (audio) in xcode. Still, audio will stop when application enters to background. What I am missing?
     
  9. elektronische

    elektronische

    Joined:
    Nov 27, 2014
    Posts:
    43
    Hi, do you know if this works too with the GPS Location Services? Or just with sound? Thanks!
     
  10. orenfridman

    orenfridman

    Joined:
    Nov 25, 2015
    Posts:
    2
    Help please! How do I get the audio running in background (iOS)?
     
  11. aihodge

    aihodge

    Joined:
    Nov 23, 2014
    Posts:
    101
    For anyone else coming upon this thread, the trick is to register the app for remote control events when configuring the AVAudioSession. Updated my previously posted code for native media playback in iOS 10.
     
  12. pixxelbob

    pixxelbob

    Joined:
    Aug 26, 2014
    Posts:
    111
    @aihodge thanks for your help.
    I've tried what you suggested in didFinishLaunchingWithOptions but I get errors in xcode.

    //edit
    Doh, the forum has removed whitespace in the code you posted :)

    I still don't get audio in the background unfortunately.
     
    Last edited: Nov 1, 2016
  13. RealWorld

    RealWorld

    Joined:
    Sep 3, 2014
    Posts:
    17
    I've tried all of the above:
    • The background mode in the plist file
    • The code change in the app controller
    • The background setting to custom in player preferences.
    Still having my audio stop when the screen is turned off however :(
    Anyone got any other ideas?
     
  14. mbaker

    mbaker

    Joined:
    Jan 9, 2013
    Posts:
    3
    @RealWorld I'm actually working through this problem for an app right now. @aihodge's approach still works. You just have to call that code in a different location. It seems that Unity now initializes it's audio category at some point after didFinishLaunchingWithOptions is executed.

    If you move the code to the start of the function applicationWillResignActive() your audio should continue to play. There's probably a better location to put this (I'm still working through a proper solution) but this should at least get you the behaviour you desire. I've also noticed that the iOS playback controls don't work with this approach. I'll have to look into that as well.

    I'm working in Unity 5.6.0f3

    I'll update this thread if I make any progress.
     
    Last edited: May 25, 2017 at 12:56 AM
  15. mbaker

    mbaker

    Joined:
    Jan 9, 2013
    Posts:
    3
    Quick update, found a better place to set the audio category. Put @aihodge's code at the bottom of the
    startUnity() method. In retrospect this seems really obvious.

    For my app I actually don't want the user to be able to control the audio from the OS level controls so it was just a matter of removing
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
     
    Last edited: May 26, 2017 at 7:01 PM