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.
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”.
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.
You may need to configure the AVAudioSession in the Xcode project too. Try putting this in UnityAppController.mm’s didFinishLaunchingWithOptions:
Updated October 6, 2016
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlaybackerror:nil];
[[AVAudioSession sharedInstance] setActive: YESerror: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
See this page in Apple’s documentation concerning background audio: Technical Q&A QA1668: Playing media while in the background using AV Foundation on iOS
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!
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!
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.
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?
Hi, do you know if this works too with the GPS Location Services? Or just with sound? Thanks!
Help please! How do I get the audio running in background (iOS)?
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.
@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.
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?
@ 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.
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];
thanks mbaker and aihodge ! think this is finally working for me ending with this last entry in the thread (re: startUnity)
Dear @mfgage @mbaker1 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
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.
Thanks @mbaker1 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
yeah, it works.
It can play the audio in the background, but how can the user control the audio from the lockscreen?