How to allow other audio sources on an iOS app

Hello there! I have a game that’s been out for some time, but I’ve received many reviews asking for allowing podcasts to play while they play the game. The “Mute other audio sources” option appears to have no effect while using FMOD and I’ve read that you need to set AVAudioSession to ambient to make this happen. The problem right now is I cannot find any sort of step by step instruction on where or how to implement this. Has anyone successfully allowed other sources to play through on an iOS app? If so, I would love to know what code to put where to make this happen!
Thanks!

Seems like the AVAudioSession is the correct answer. It is usually configured in the app delegate. Just look through apple documentation - it is quite extensive and has example code included. While not being a step by step tutorial path, it is a great opportunity to learn an ios app lifecycle and audiosession basics so that further issues are easier to understand and resolve.

AVAudioSession ref:
https://developer.apple.com/documentation/avfoundation/avaudiosession

For a deeper dive learn this guide:
https://developer.apple.com/library/archive/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007875

which is a part of AVFoundation:
https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40010188

1 Like

Thank you Roma.

In case any one else runs into this error in the future, I found a solution that works for me. In the Xcode generated unity project open up the UnityAppController.mm file in the classes folder. From there scroll down to line 143 (might be different when you are reading this), but add the second line below to your code.

AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient error: nil];
[audioSession setActive: YES error: nil];
1 Like

I ran into this problem and found that splinedrew’s solution doesn’t work anymore. I found that the session category needs to be set in the didFinishLaunchingWithOptions overload of the application function, doing it in startUnity is too late.

We use automation for our builds, meaning that we can’t have someone manually edit UnityAppController.mm every time we make a new build. I wrote a script which automatically applies the patch whenever we build. I’m sharing it below for others who also encounter this issue. It’s crude but it works, at least at the time of writing.

using System.IO;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;

public class FMODPostProcessor : IPostprocessBuildWithReport
{
	public int callbackOrder => 0;

	public void OnPostprocessBuild(BuildReport report)
	{
		#if UNITY_IOS
		const string TEXT_TO_FIND = "::printf(\"-> applicationDidFinishLaunching()\\n\");";
		const string TEXT_TO_REPLACE = "::printf(\"-> applicationDidFinishLaunching()\\n\");\n    AVAudioSession* audioSession = [AVAudioSession sharedInstance];\n   [audioSession setCategory:AVAudioSessionCategoryAmbient error: nil];\n";
		string unityAppControllerPath = $"{report.summary.outputPath}/Classes/UnityAppController.mm";

		if (File.Exists(unityAppControllerPath))
		{
			string source = File.ReadAllText(unityAppControllerPath);
			source = source.Replace(TEXT_TO_FIND, TEXT_TO_REPLACE);
			File.WriteAllText(unityAppControllerPath, source);
			Debug.Log($"[FMODPostProcessor] Set the iOS audio category to ambient");
		}
		else
		{
			Debug.LogError($"[FMODPostProcessor] Could not find the file \"{unityAppControllerPath}\"");
		}
		#endif
	}
}