Search Unity

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:
    24
    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:
    157
    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".
     
    ilmario likes this.
  3. mfgage

    mfgage

    Joined:
    Jun 4, 2013
    Posts:
    24
    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:
    157
    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
    5argon likes this.
  5. mfgage

    mfgage

    Joined:
    Jun 4, 2013
    Posts:
    24
    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. makeatemplate

    makeatemplate

    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:
    24
    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. diegoadrada

    diegoadrada

    Joined:
    Nov 27, 2014
    Posts:
    56
    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:
    4
    Help please! How do I get the audio running in background (iOS)?
     
  11. aihodge

    aihodge

    Joined:
    Nov 23, 2014
    Posts:
    157
    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.
     
    ilmario likes this.
  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. Deleted User

    Deleted User

    Guest

    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:
    17
    @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
    ilmario likes this.
  15. mbaker

    mbaker

    Joined:
    Jan 9, 2013
    Posts:
    17
    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
    Deleted User likes this.
  16. mfgage

    mfgage

    Joined:
    Jun 4, 2013
    Posts:
    24
    thanks mbaker and aihodge ! think this is finally working for me ending with this last entry in the thread (re: startUnity)
     
    mbaker and aihodge like this.
  17. mkurnadi

    mkurnadi

    Joined:
    Jan 29, 2016
    Posts:
    6
    Dear @mfgage @mbaker or @aihodge

    Thanks for the tips, we are able to run our audio streaming IOS in the background as well. Just wondering what type of audio player do you guys use ? Native IOS, Unity Player or Third Party Unity Assets ?
    Also last question, did any of your IOS application pass the Apple Itunes Approval to be released ? Or your solution only pass Apple Testflight ?

    Thanks,

    Martin
     
  18. mbaker

    mbaker

    Joined:
    Jan 9, 2013
    Posts:
    17
    On iOS I just used the audio playback built into Unity (AudioSource). In my specific case I was playing multi hour long audio tracks so my audio files were loaded from the StreamingAssets folder with WWW.GetAudioClip().

    I didn't have any issues getting approval from Apple. The app is live on the app store today.
     
  19. mkurnadi

    mkurnadi

    Joined:
    Jan 29, 2016
    Posts:
    6
    Thanks @mbaker for sharing. My audio IOS application can run in the background after inserting the above solution at the bottom of the startUnity() method, however behavior of the application changes after the application gets interrupted by other application (for example Clock Timer Alarm or other application). The AVAudioSession category was no longer AVAudioSessionCategoryPlayback, and the application can no longer run in the backgrond. I believe the category switched to AVAudioSessionCategorySoloAmbient(Default). For reference : Definition of category here
     
  20. Deleted User

    Deleted User

    Guest

    yeah, it works.
    It can play the audio in the background, but how can the user control the audio from the lockscreen?
     
    Last edited by a moderator: Jan 18, 2018
  21. EvalDaemon

    EvalDaemon

    Joined:
    Aug 8, 2013
    Posts:
    101
    I confirm that changing the UnityAppController.mm StartUnity method does allow the music (I'm using FMOD) to play in the background after the home button is pressed. At least on the one iPad I have tested so far.

    Code (CSharp):
    1. - (void)startUnity:(UIApplication*)application
    2. {
    3.     NSAssert(_unityAppReady == NO, @"[UnityAppController startUnity:] called after Unity has been initialized");
    4.  
    5.     UnityInitApplicationGraphics();
    6.  
    7.     // we make sure that first level gets correct display list and orientation
    8.     [[DisplayManager Instance] updateDisplayListInUnity];
    9.  
    10.     UnityLoadApplication();
    11.     Profiler_InitProfiler();
    12.  
    13.     [self showGameUI];
    14.     [self createDisplayLink];
    15.  
    16.     UnitySetPlayerFocus(1);
    17.    
    18.     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    19. [[AVAudioSession sharedInstance] setActive:YES error: nil];
    20. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    21. }
    22.  
     
  22. EvalDaemon

    EvalDaemon

    Joined:
    Aug 8, 2013
    Posts:
    101
    And don't forget to add

    Code (CSharp):
    1. @import AVFoundation;
    to UnityAppController.mm declaration section.
     
    Kofiro and 5argon like this.
  23. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,539
    For me its `#include <AVFoundation/AVFoundation.h>` but other than that background audio now works correctly. Thanks!
     
    azporispo, elabor-han and aihodge like this.
  24. GMR101

    GMR101

    Joined:
    Jan 31, 2015
    Posts:
    1
    Does this allow users to control the audio with the media controls from the lock screen? If not: how?
     
  25. prawn-star

    prawn-star

    Joined:
    Nov 21, 2012
    Posts:
    48
    Hi GMR101

    You can get the media controls to work from the lock screen. I've been working on a project that required this.
    I ended up bypassing Unity's audio system and writing a plugin that creates and handles a native MPMoviePlayerController to play the apps audio (It can play audio only, doesn't require a video).
    I put all the code to handle the background audio in the function that plays the audio.
    I also stop the sharing when the audio is stopped (not paused)
    [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];

    To get the media controls setup you need to look into
    MPRemoteCommandCenter
    and
    MPNowPlayingInfoCenter
     
  26. ramon_delmondo

    ramon_delmondo

    Joined:
    Aug 19, 2015
    Posts:
    22
    Hi

    @aihodge
    I've tried what you suggested but i got this errors

    Someone can help me?

    40403645_1866730926739074_3402534771916537856_n.png 40352586_2710813359144842_3040125281156202496_n.png
     
  27. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,539
    Press Cmd+Control and follow into your header file and check if the interface is really there? This is my AVAudioSession.h which you have also included.

    Screenshot 2018-08-30 18.32.44.png

    If it is there just try clean and rebuild, or close Xcode and come back etc. Or choose "Replace" when exporting from Unity just in case.
     
  28. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,084
    no, the interface in the form he blindly copy pasted the source in is not there
    all he's missing is correct number of arguments - I.E. adding spaces where needed
     
    5argon likes this.
  29. aihodge

    aihodge

    Joined:
    Nov 23, 2014
    Posts:
    157
    Hi @ramon_delmondo, look at the method definitions... you're missing the space between setting the audio session category and the next argument for the error. Likewise in the second line, you're missing a space between passing in the boolean for setActive and the error argument. Fix those two things and assuming everything else in the project is place/correct, it should work :)
     
  30. Soternicus

    Soternicus

    Joined:
    Oct 30, 2016
    Posts:
    7
    Hi, I followed all the steps and it worked instantly, but I play audio in loop, so, as soon as the audio track ends, it stops, even with the loop ative.

    I really appreciate all the help found here, you are all the best! Thank you.
     
    aihodge likes this.
  31. thao050192

    thao050192

    Joined:
    Jul 17, 2017
    Posts:
    5
    im not pro code, plz someones help me example project, thank all
     
  32. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    977
    ca
    can you include a sample project with just the barebones background audio working. perhaps just a simple gameobject with looping audio. also which version of Unity?
     
  33. hatibnb_unity

    hatibnb_unity

    Joined:
    Mar 2, 2021
    Posts:
    1
    1. After changing the UnityAppController.mm StartUnity method there is a setting at unity end that worked for me just in case any one still facing the issue .
    2. (In Unity Editor go to Build Setting > Other Setting >Enable Custome Background Behaviors >enable audio,airplay,Pip > Enable Background fetch
    3. Unity Version is 2019.4.8f1

      - (void)startUnity:(UIApplication*)application
    4. {
    5. NSAssert(_unityAppReady == NO, @"[UnityAppController startUnity:] called after Unity has been initialized");

    6. UnityInitApplicationGraphics();

    7. // we make sure that first level gets correct display list and orientation
    8. [[DisplayManager Instance] updateDisplayListInUnity];

    9. UnityLoadApplication();
    10. Profiler_InitProfiler();

    11. [self showGameUI];
    12. [self createDisplayLink];

    13. UnitySetPlayerFocus(1);
    14. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    15. [[AVAudioSession sharedInstance] setActive:YES error: nil];
    16. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    17. }
     

    Attached Files:

  34. jamesjenningsard

    jamesjenningsard

    Joined:
    Jun 26, 2020
    Posts:
    6
    Hi friends

    Everything in this thread seems to work brilliantly with Unity's default-engine-audio.

    Has anybody had anything close to success in regards to "background audio" with Wwise?
     
  35. ReaktorDave

    ReaktorDave

    Joined:
    May 8, 2014
    Posts:
    131
    While the background playback seems to be working with the methods described in this thread, as soon as you turn of the screen, all your MonoBehaviours will stop updating and so any interactivy that you have scripted into the music will become unavailable. As soon as the screen goes on, the MonoBehaviours seem to continue.

    Any idea what we can do to keep our scripts active while the iPhone's screen is off?
     
  36. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    104
    Don't forget this line of code:

    Application.runInBackground = true;
     
  37. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    104
    Everything works perfectly, but I wish I could control (Stop/Resume) the audio... :(

    ¿Any method for that?
     
  38. ReaktorDave

    ReaktorDave

    Joined:
    May 8, 2014
    Posts:
    131
unityunity