ryanheise / audio_session

MIT License
117 stars 83 forks source link

Can't release microphone on iOS after opening audio session for recording #71

Closed RishiKar closed 2 years ago

RishiKar commented 2 years ago

Hi @ryanheise , I have been stuck at trying to identify why I can't get the microphone to be release after stopping recording. A user can see this in the form of an orange dot on iOS devices (indicating mic is in use). I am initialising an audio session usingaudio_session. Then I record using flutter_sound. On stop, I make sure I call stopRecorder, closeRecorder and close and cancel all streams. Is there a way to use AudioSession.instance.setActive(false) in the dispose call or some other way? This is audio_session initialisation:

 void initRecorder() async {
    await _createRecordingFile.openRecorder();
    await _recorder.openRecorder();
    final session = await AudioSession.instance;
    await session.configure(AudioSessionConfiguration(
      avAudioSessionCategory: AVAudioSessionCategory.record,
      avAudioSessionCategoryOptions:
          AVAudioSessionCategoryOptions.allowBluetooth,
      avAudioSessionMode: AVAudioSessionMode.spokenAudio,
      avAudioSessionRouteSharingPolicy:
          AVAudioSessionRouteSharingPolicy.defaultPolicy,
      avAudioSessionSetActiveOptions:
          AVAudioSessionSetActiveOptions.notifyOthersOnDeactivation,
      androidAudioAttributes: AndroidAudioAttributes(
          contentType: AndroidAudioContentType.speech,
          flags: AndroidAudioFlags.none,
          usage: AndroidAudioUsage.voiceCommunication),
      androidAudioFocusGainType: AndroidAudioFocusGainType.gain,
      androidWillPauseWhenDucked: true,
    ));
  }

(Edited by @ryanheise to correctly format multiline code blocks)

ryanheise commented 2 years ago

Are you sure it's not flutter_sound that's not releasing the microphone?

RishiKar commented 2 years ago

Hi Ryan, I verified my code again and aggressively called all stop and cancel methods.

void stopAndRelease() async {
    await _createRecordingFile.stopRecorder();
    await _recorder.stopRecorder();
    await _createRecordingFile.closeRecorder();
    await _recorder.closeRecorder();
    await _audioStream.close();
    await _recordingDataController.close();
    await _recordingDataSubscription.cancel();
  }

I got this from the logs so but was not able to understand them

[VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: PlatformException(2003329396, The operation couldn’t be completed. (OSStatus error 2003329396.), null, null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
<asynchronous suspension>
#2      AudioSession.configure (package:audio_session/src/core.dart:195:5)
<asynchronous suspension>
#3      AudioPlayerService.initAudioSession (package:sixthousandthoughts/services/audioplayer_service.dart:15:5)
<asynchronous suspension>

(Edited by @ryanheise to format multiline code blocks)

ryanheise commented 2 years ago

I found this S/O answer: https://stackoverflow.com/questions/21893007/how-to-consistently-stop-avaudiosession-after-avspeechutterance

It basically says you may get this error if you try to deactivate your audio session too soon before the audio sources have been completely closed.

RishiKar commented 2 years ago

Thanks Ryan, I am not sure how I can check whether it is too soon but I will review the code to reorder the close/cancel methods and check again to make sure I am waiting for them to finish.

Is it ok if I keep the issue open until then? If you have any suggestions or have faced this in any of your work, I would be very grateful for any directions on this.

ryanheise commented 2 years ago

I would also check that flutter_sound also actually releases the resources before the session is deactivated. This may involve looking at flutter_sound's code. A quick test you can do is to insert a delay before deactivating the session and see if it has any effect.

RishiKar commented 2 years ago

Hi @ryanheise , It was not related to audio session plug-in . I believe it had something to do with not canceling the recording stream correctly. For now, I can see that the mic resources are being released correctly. 🤞 I will close the issue. Thank you for your help and suggestions.