playSound method

int playSound(
  1. Sound sound, {
  2. int channel = -1,
  3. bool loop = false,
  4. double? loopStart,
  5. double? loopEnd,
})

Plays the given Sound.

  • sound: The Sound object to play.
  • channel: The specific channel ID to play the sound on. If -1 (default), an available channel will be chosen automatically from all channels.
  • loop: Whether the sound should loop. Defaults to false.
  • loopStart: The start time in samples for looping, if loop is true. Converted to seconds internally using the sound's sample rate. Requires loopEnd to also be set.
  • loopEnd: The end time in samples for looping, if loop is true. Converted to seconds internally using the sound's sample rate. Requires loopStart to also be set.

Returns the channel ID on which the sound is playing, or -1 if the sound could not be played (e.g., sound not ready, retrigger delay not met, or no free channels).

Implementation

int playSound(Sound sound, {int channel = -1, bool loop = false, double? loopStart, double? loopEnd}) {
  var targetChannels = (channel == -1) ? _allChannels : [channel];
  return playSoundOnTargetChannels(
    sound,
    targetChannels: targetChannels,
    loop: loop,
    loopStart: loopStart,
    loopEnd: loopEnd,
  );
}