Where do I setup AVAudioSession Category Play And Record for iOS?

Where do I setup AVAudioSessionCategoryPlayAndRecord for iOS?
Got a link that is very general, but doesn’t give me an example.

All I keep getting is this link: https://developer.apple.com/library/archive/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionBasics/AudioSessionBasics.html

Which is a documentation from Apple, I need a code example for Unity…
Or, if it can only be done on the xCode project exported by Unity, I need to know where I change it, but this documentation shows nothing…

Found the solution myself, but I think I should show you all an example of how a clear answer should look like, instead of just telling one he has to change the AVAudioSession category but not telling him how he is supposed to do it.
So in order to record on iOS devices, after you create a build, which is an xCode project, because that is how Unity is exporting to iOS, you don’t get an ipa file but an xCode project you copy to a mac and open in xCode. So before you open the project in xCode, you go to the Classes folder in this project, there you can see a file by the name of: UnityAppController.mm

open it you will see a function startUnity, in that function after this line of code: UnitySetPlayerFocus(1);
you add this line:
[[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

like so:

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

    UnityInitApplicationGraphics();

    // we make sure that first level gets correct display list and orientation
    [[DisplayManager Instance] updateDisplayListCacheInUnity];

    UnityLoadApplication();
    Profiler_InitProfiler();

    [self showGameUI];
    [self createDisplayLink];

    UnitySetPlayerFocus(1);

    [[AVAudioSession sharedInstance]
	setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

    AVAudioSession* audioSession = [AVAudioSession sharedInstance];
    [audioSession setActive: (UnityShouldActivateAVAudioSession() == 1) error: nil];
    [audioSession addObserver: self forKeyPath: @"outputVolume" options: 0 context: nil];
    UnityUpdateMuteState([audioSession outputVolume] < 0.01f ? 1 : 0

See, that is an answer worthy of its name, wasn’t too hard was it?

1 Like

Thank you for sharing your solution. I agree our iOS docs do not provide enough information on how to set the AVAudioSession category and it looks like this is a common source of confusion. I have created a task to add a code example on how to set the AVAudioSession category, please let us know if you come across anything else that needs improving!