stopRecording method

Future<Uint8List?> stopRecording()

Stop capture. For a startRecordingToBuffer session, returns the accumulated WAV bytes (and deletes the temp file); for a chunk-stream session, closing the recorder ends the stream (which lets the SDK session flush its final result) and this returns null. Mirrors Swift stopRecording().

Implementation

Future<Uint8List?> stopRecording() async {
  if (!_isRecording) {
    return null;
  }
  _stopAudioLevelMonitoring();
  String? path;
  try {
    path = await _recorder.stop();
  } catch (e) {
    _logger.error('Failed to stop capture: $e');
  }
  _isRecording = false;

  final recordingPath = _currentRecordingPath;
  _currentRecordingPath = null;
  if (recordingPath == null) {
    // Chunk-stream session: no file involved.
    return null;
  }

  final file = File(path ?? recordingPath);
  if (!await file.exists()) {
    _logger.error('Recording file does not exist: ${file.path}');
    return null;
  }
  final audioData = await file.readAsBytes();
  try {
    await file.delete();
  } catch (e) {
    _logger.warning('Failed to cleanup temp recording file: $e');
  }
  return audioData;
}