playBeep static method

Future<void> playBeep({
  1. bool success = true,
})

Implementation

static Future<void> playBeep({bool success = true}) async {
  const String pluginName = 'basic_beep';
  final String soundFile = success ? 'beep.mp3' : 'negative_beep.mp3';
  final String audioUrl =
      'assets/packages/$pluginName/assets/sounds/$soundFile';

  final HTMLAudioElement audio = HTMLAudioElement();
  audio.src = audioUrl;

  document.body?.append(audio);

  try {
    await audio.play().toDart;
  } catch (e) {
    debugPrint('Error playing beep sound: $e');
  } finally {
    audio.addEventListener(
      'ended',
      (Event _) {
        audio.remove();
      }.toJS,
    );

    audio.addEventListener(
      'error',
      (Event _) {
        debugPrint('An error occurred during beep playback.');
        audio.remove();
      }.toJS,
    );
  }
}