startRecordingToBuffer method

Future<String?> startRecordingToBuffer({
  1. int sampleRate = 16000,
  2. int numChannels = 1,
})

One-shot convenience: record to a temp WAV file until stopRecording is called. Returns the recording path, or null on failure.

Implementation

Future<String?> startRecordingToBuffer({
  int sampleRate = 16000,
  int numChannels = 1,
}) async {
  if (_isRecording) {
    await stopRecording();
  }
  if (!await _recorder.hasPermission()) {
    _logger.warning('Microphone permission not granted');
    return null;
  }
  try {
    final tempDir = await getTemporaryDirectory();
    final timestamp = DateTime.now().millisecondsSinceEpoch;
    _currentRecordingPath = '${tempDir.path}/runanywhere_rec_$timestamp.wav';
    await _recorder.start(
      RecordConfig(
        encoder: AudioEncoder.wav,
        sampleRate: sampleRate,
        numChannels: numChannels,
        bitRate: 128000,
      ),
      path: _currentRecordingPath!,
    );
    _isRecording = true;
    _startAudioLevelMonitoring();
    return _currentRecordingPath;
  } catch (e) {
    _logger.error('Failed to start recording: $e');
    _isRecording = false;
    _currentRecordingPath = null;
    return null;
  }
}