stopRecording method

Future<void> stopRecording({
  1. Object? withError,
})

Stops recording and releases audio capture resources.

If withError is provided, agentReadyFuture completes with that error. Otherwise, agentReadyFuture completes successfully (if not already completed).

Implementation

Future<void> stopRecording({Object? withError}) async {
  if (!_isRecording) return;
  _isRecording = false;

  // Cancel the stream subscription.
  await _streamSubscription?.cancel();
  _streamSubscription = null;

  // Stop the audio capture.
  await _audioCapture?.stop();
  _audioCapture = null;

  // Stop native recording session if it was started.
  if (_nativeRecordingStarted) {
    await webrtc.NativeAudioManagement.stopLocalRecording();
  }

  _nativeRecordingStarted = false;

  // Complete agent ready future if not already completed
  withError != null ? _agentReadyManager.completeError(withError) : _agentReadyManager.complete();

  // Emit the stopped event
  _room.events.emit(PreConnectAudioBufferStoppedEvent(
    bufferedSize: _buffer.length,
    isBufferSent: _isBufferSent,
  ));

  logger.info('[Preconnect audio] stopped recording');
}