ensureAudio method

Future<XCameraController?> ensureAudio()

Reopens the current lens with audio, retaining silent capture on failure.

Implementation

Future<XCameraController?> ensureAudio() => _enqueue(() async {
  final current = _controller;
  if (current == null || current.enableAudio) return current;
  final description = current.description;
  final preset = current.resolutionPreset;
  _publish(null);
  await current.dispose();
  if (_closed) return null;

  XCameraController? next;
  try {
    next = await _open(description, preset, audio: true);
  } on XCameraException catch (error, stack) {
    Log.tag('camera')
        .w('audio camera failed', error: error, stackTrace: stack);
    if (_closed) return null;
    next = await _open(description, preset, audio: false);
  }
  return next;
});