Search Unity

Bluetooth headset does not output game audio if mic is recording

Discussion in 'iOS and tvOS' started by Adam_Weesner, Jul 15, 2021.

  1. Adam_Weesner

    Adam_Weesner

    Joined:
    Aug 6, 2020
    Posts:
    18
    I'm using Unity 2020.1.17f1.

    My game requires the mic to record at all times. However, I cannot get a bluetooth headset to output game audio while recording from a mic concurrently.

    I either get:
    • Game audio works on the bluetooth headset, but no mic input.
    • Mic input works, but game audio is output on the phone's internal speakers.
    I followed this thread (https://answers.unity.com/questions/1520558/ios-app-does-not-play-audio-from-bluetooth-headset.html) and imported this IOS speaker fix (https://github.com/reivendark/UnitySpeakerFix_iOS7plus). I also use an IOS Unity plugin called AudioSession. When I initialize the game, I call a function from AudioSession like this:

    Code (CSharp):
    1. AudioSession.SetAudioPlayAndRecord();
    2. iPhoneSpeaker.ForceToSpeaker();
    The first line executes this code:

    Code (CSharp):
    1. void _SetAudioPlayAndRecord(){          
    2.     AVAudioSession *session = [AVAudioSession sharedInstance];
    3.     double rate = 24000.0; // This should match System::setSoftwareFormat 'samplerate' which defaults to 24000
    4.     int blockSize = 512; // This should match System::setDSPBufferSize 'bufferlength' which defaults to 512
    5.    
    6.     if (@available(iOS 13.0, *)) {
    7.         [session setAllowHapticsAndSystemSoundsDuringRecording:true error:nil];
    8.     }
    9.    
    10.     BOOL success = [session setPreferredSampleRate:rate error:nil];
    11.     assert(success);
    12.    
    13.     success = [session setPreferredIOBufferDuration:blockSize / rate error:nil];
    14.     assert(success);
    15.    
    16.     success = [session setActive:TRUE error:nil];
    17.     assert(success);
    18.    
    19.     success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    20.     assert(success);
    21. }
    The second line belongs to that IOS speaker fix. If I comment out the SetAudioPlayAndRecord() call, then the bluetooth headset successfully outputs game audio. However, I don't get mic input. If I uncomment, then the mic input works, but the phone's internal speakers play game audio.

    Regarding Unity Player Settings, I've disabled "Prepare iOS for Recording" and "Force iOS Speakers when Recording". I've experimented with turning these on/off, but they don't help the issue.

    Any ideas on how to fix this? Cheers.
     
  2. Adam_Weesner

    Adam_Weesner

    Joined:
    Aug 6, 2020
    Posts:
    18
    This has become quite a blocking issue for me, so I'd like to get this resolved soon.
     
  3. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    168
    Did you figure this out? I can't get bluetooth sound to work
     
  4. Woelfle

    Woelfle

    Joined:
    Jan 13, 2019
    Posts:
    4
    Did somebody got this bluetooth microphone issue resolved somehow ?
     
  5. aBethke

    aBethke

    Joined:
    Jun 5, 2015
    Posts:
    13
    Has anyone looked into using FMOD to get around this issue and if so did you have any luck?
     
  6. dave_m_moore

    dave_m_moore

    Joined:
    Jul 28, 2016
    Posts:
    39
    Does anyone have a fix for this bug please?
     
  7. aBethke

    aBethke

    Joined:
    Jun 5, 2015
    Posts:
    13
    Nope. Still looking myself.

    One thing I'm currently looking into is how Unity Vivox is handling app configuration to do this. I saw some references to the types of settings we'd want to change to make this work via a native extension but not enough of the actual code to get started myself.
     
  8. dave_m_moore

    dave_m_moore

    Joined:
    Jul 28, 2016
    Posts:
    39
    I've been have a closer look at this. In my situation, I am speculating that it's to do with the way that Unity implements the speech recognition capability (which is the prime reason for me needing a headset microphone). It seems that when built, the Unity application spawn off another microsoft application to handle the speech recognition. Seems to me that it is not possible for both the Unity App and the microsoft speech recognition app to have control over the bluetooth device at the same time (only one at a time). Interesting, I could get it to work in the editor but not for an application build (on Windows).