recreateNode method

void recreateNode()

Implementation

void recreateNode() {
  if (currentUrl == null) {
    return;
  }

  final p = player = AudioElement(currentUrl);
  // As the AudioElement is created dynamically via script,
  // features like 'stereo panning' need the CORS header to be enabled.
  // See: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
  p.crossOrigin = 'anonymous';
  p.loop = shouldLoop();
  p.volume = currentVolume;
  p.playbackRate = currentPlaybackRate;

  // setup stereo panning
  final audioContext = JsAudioContext();
  final source = audioContext.createMediaElementSource(player!);
  stereoPanner = audioContext.createStereoPanner();
  source.connect(stereoPanner!);
  stereoPanner?.connect(audioContext.destination);

  playerPlaySubscription = p.onPlay.listen((_) {
    streamsInterface.emitDuration(
      playerId,
      p.duration.fromSecondsToDuration(),
    );
  });
  playerLoadedDataSubscription = p.onLoadedData.listen((_) {
    streamsInterface.emitDuration(
      playerId,
      p.duration.fromSecondsToDuration(),
    );
  });
  playerTimeUpdateSubscription = p.onTimeUpdate.listen((_) {
    streamsInterface.emitPosition(
      playerId,
      p.currentTime.fromSecondsToDuration(),
    );
  });
  playerSeekedSubscription = p.onSeeked.listen((_) {
    streamsInterface.emitSeekComplete(playerId);
  });
  playerEndedSubscription = p.onEnded.listen((_) {
    pausedAt = 0;
    player?.currentTime = 0;
    streamsInterface.emitComplete(playerId);
  });
}