Thanks again for the video, super invaluable for understanding what’s going on.
On my end, what seems to fix this issue is to set the preferred buffer size and sample rate for the audio session alongside setting the category, which should ensure that when the FMOD Unity app is foregrounded that iOS gives it the buffer size and sample rate it wants.
You’ll need to make modifications to the following files to do this:
./Assets/Plugins/FMOD/platform_ios.mm
Remove the previous change I suggested where AVAudioSessionCategorySoloAmbient
is set, and add the following new function:
extern "C" void SetAudioSessionSettings(double rate, int blockSize)
{
NSLog(@"Sample rate = %lf", rate);
NSLog(@"Block size = %d", blockSize);
AVAudioSession *session = [AVAudioSession sharedInstance];
NSLog(@"Setting AVAudioSessionCategory to SoloAmbient");
NSError *audioSessionError = nil;
[session setCategory:AVAudioSessionCategorySoloAmbient error:&audioSessionError];
if (audioSessionError)
{
NSLog(@"Error setting AVAudioSessionCategory: %@", audioSessionError);
}
double bufferDuration = blockSize / rate;
NSLog(@"Setting PreferredIOBufferDuration to %lf", bufferDuration);
[session setPreferredIOBufferDuration:bufferDuration error:&audioSessionError];
if (audioSessionError){
NSLog(@"Error setting PreferredIOBufferDuration: %@", audioSessionError);
}
NSLog(@"Setting PreferredSampleRate to %lf", rate);
[session setPreferredSampleRate:rate error:&audioSessionError];
if (audioSessionError)
{
NSLog(@"Error setting PreferredSampleRate: %@", audioSessionError);
}
}
./Assets/Plugins/FMOD/src/RuntimeManager.cs
Use DllImport
to export the new function at line 1462:
#if (UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS) && !UNITY_EDITOR
// new code start
[DllImport("__Internal")]
private static extern void SetAudioSessionSettings(double rate, int blockSize);
// new code end
[DllImport("__Internal")]
private static extern void RegisterSuspendCallback(Action<bool> func);
#endif
And then call the new function after the FMOD System has been initialized, at line 408:
#if (UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS) && !UNITY_EDITOR
// new code start
int systemSampleRate;
coreSystem.getSoftwareFormat(out systemSampleRate, out _, out _);
uint systemDSPBufferLength;
coreSystem.getDSPBufferSize(out systemDSPBufferLength, out _);
SetAudioSessionSettings((double) systemSampleRate, (int)systemDSPBufferLength);
// new code end
RegisterSuspendCallback(HandleInterrupt);
#endif
There does appear to be an edge case where backgrounded audio being played by Brave starts playing while the FMOD Unity application is active and in the foreground - this is less an issue with FMOD, and more an issue with how Brave is handling playing in the background. In this case, FMOD audio will be forced to suspend even while the app is open, but should resume if the app is suspended and resumed again.
Give those changes a try and let me know whether that resolves the issue!