open method

  1. @override
Future<void> open({
  1. required String path,
  2. required String audioType,
  3. String? package,
  4. bool autoStart = false,
  5. double volume = 1,
  6. double? seek,
  7. double? playSpeed,
  8. Map? networkHeaders,
})
override

Implementation

@override
Future<void> open({
  required String path,
  required String audioType,
  String? package,
  bool autoStart = false,
  double volume = 1,
  double? seek,
  double? playSpeed,
  Map? networkHeaders,
}) async {
  stop();
  _durationMs = null;
  _position = null;
  _audioElement = html.AudioElement(findAssetPath(
    path,
    audioType,
    package: package,
  ));

  // it seems html audielement cannot take networkHeaders :'(

  _onEndListener = _audioElement?.onEnded.listen((event) {
    channel.invokeMethod(WebPlayer.methodFinished, true);
  });

  _onCanPlayListener = _audioElement?.onCanPlay.listen((event) {
    if (autoStart) {
      play();
    }

    this.volume = volume;
    final durationMs = (_audioElement?.duration ?? 0) * 1000;

    if (durationMs != _durationMs) {
      _durationMs = durationMs;
      channel.invokeMethod(WebPlayer.methodCurrent, {'totalDurationMs': durationMs});
    }

    if (seek != null) {
      this.seek(to: seek);
    }

    if (playSpeed != null) {
      this.playSpeed = playSpeed;
    }

    // single event
    _onCanPlayListener?.cancel();
    _onCanPlayListener = null;
  });
}