acquire method

Stream<void> acquire(
  1. AudioMode mode, {
  2. AudioSpeakerKind? speaker,
})

Acquires and returns the handle for the provided AudioMode.

The returned Stream must be listened to when mode is still needed, and released when the mode is no longer needed.

speaker can be set to transition the currently selected one into that when intent is acquire for the first time.

Implementation

Stream<void> acquire(AudioMode mode, {AudioSpeakerKind? speaker}) {
  final _AudioIntent intent = _AudioIntent(mode, speaker: speaker);

  final StreamController<void> controller = StreamController.broadcast(
    onListen: () async {
      Log.debug(
        'acquire($mode) -> onListen for ${intent.id} with `${speaker?.name}`',
        '$runtimeType',
      );

      _intents[intent.id] = intent;
      await reconfigure(preferredSpeaker: speaker);
    },
    onCancel: () async {
      Log.debug(
        'acquire($mode) -> onCancel for ${intent.id}',
        '$runtimeType',
      );

      _intents.remove(intent.id);
      await reconfigure();
    },
  );

  _controllers[intent.id] = controller;

  return controller.stream;
}