startCapture method
Starts capturing system audio.
This method will request necessary permissions (screen recording on macOS) and begin capturing audio from the system output.
config is optional. If provided, it will update the current configuration
before starting capture.
Throws an Exception if:
- Permissions are not granted
- Capture fails to start
Example:
final capture = SystemAudioCapture();
try {
await capture.startCapture(
config: SystemAudioConfig(
sampleRate: 44100,
channels: 2,
),
);
print('System audio capture started');
} catch (e) {
print('Failed to start: $e');
}
Implementation
Future<void> startCapture({SystemAudioConfig? config}) async {
if (_isRecording) {
return;
}
if (config != null) {
updateConfig(config);
}
try {
await requestPermissions();
final started = await _channel.invokeMethod<bool>(
_SystemAudioMethod.startCapture.name,
_config.toMap(),
);
if (started != true) {
throw Exception('Failed to start system audio capture');
}
// Listen to audio stream
_audioStream = _audioStreamChannel.receiveBroadcastStream().map((
dynamic event,
) {
if (event is Uint8List) {
return event;
} else if (event is List<int>) {
return Uint8List.fromList(event);
}
throw Exception('Unexpected audio data type: ${event.runtimeType}');
});
// Status stream is created lazily via getter, no need to recreate here
_isRecording = true;
} catch (e) {
rethrow;
}
}