play method

  1. @override
Future<void> play({
  1. required String path,
  2. double volume = 1.0,
  3. bool loop = false,
})
override

Play audio from the given path

Implementation

@override
Future<void> play({
  required String path,
  double volume = 1.0,
  bool loop = false,
}) async {
  try {
    // Stop any currently playing audio
    await stop();

    // Create audio element
    _audioElement = HTMLAudioElement()..src = path;
    _audioElement!.volume = volume;
    _audioElement!.loop = loop;

    // Register this instance
    _instances[_audioElement!] = this;

    // Set up event listeners using static handlers
    _audioElement!.ontimeupdate = (_handleTimeUpdate.toJS);
    _audioElement!.ondurationchange = (_handleDurationChange.toJS);
    _audioElement!.onended = (_handleEnded.toJS);
    _audioElement!.onplay = (_handlePlay.toJS);
    _audioElement!.onpause = (_handlePause.toJS);

    // Start playback
    try {
      await _audioElement!.play().toDart;
    } catch (error) {
      throw Exception('Failed to play audio: $error');
    }
  } catch (e) {
    throw Exception('Error playing audio: $e');
  }
}