setRate method

void setRate(
  1. int channelId,
  2. double rate
)

Sets the playback rate for the sound on the specified channelId.

  • channelId: The ID of the channel whose playback rate is to be set.
  • rate: The desired playback rate. 1.0 is normal speed. 0.5 is half speed, 2.0 is double speed. If the sound is currently playing, its perceived pitch will also change. The current playback position is adjusted to maintain smooth transition if playing.

Implementation

void setRate(int channelId, double rate) {
  var channel = _channels[channelId];

  if (channel.state == ChannelState.playing) {
    var time = audioContext.currentTime;
    channel
      ..offset = (channel.offset + (time - channel.startTime) * channel.rate) % channel.sound!.buffer!.duration
      ..startTime = time;
  }

  channel
    ..rate = rate
    ..sourceNode?.playbackRate.value = rate;
}