initialize method

Future<void> initialize()

Initialize the audio session manager

Implementation

Future<void> initialize() async {
  // Only initialize once
  if (_initialized) return;

  try {
    // Set up the interruption event stream
    _interruptionEventStream = _interruptionChannel
        .receiveBroadcastStream()
        .map<InterruptionEvent>((dynamic event) {
          final Map<dynamic, dynamic> map = event as Map<dynamic, dynamic>;
          return InterruptionEvent(
            begin: map['begin'] as bool,
            type: InterruptionType.values[map['type'] as int],
          );
        });
  } catch (e) {
    // Create a dummy stream if the platform implementation is missing
    _interruptionEventStream = Stream<InterruptionEvent>.empty();
  }

  try {
    // Set up the becoming noisy event stream
    _becomingNoisyEventStream = _becomingNoisyChannel
        .receiveBroadcastStream()
        .map((_) {});
  } catch (e) {
    // Create a dummy stream if the platform implementation is missing
    _becomingNoisyEventStream = Stream<void>.empty();
  }

  // Initialize the platform side
  try {
    await _channel.invokeMethod('initialize');
  } catch (e) {
    // Silently handle the error - the platform side might not be implemented yet
  }

  // Mark as initialized to prevent further attempts
  _initialized = true;
}