Is there a way to fast forward using just_audio?

977

Solution 1

player.seek(Duration(seconds: player.position.inSeconds - 15)); rewind 15 second player.seek(Duration(seconds: player.position.inSeconds + 15)); forward 15 second'

 IconButton(
      icon: Icon(
        Icons.replay_10,
        color: Colors.white,
      ),
      iconSize: 25,
      onPressed: () async {
        await player.seek(Duration(seconds: player.position.inSeconds - 15));
      },
    )`

the player comes from AudioPlayer player

Solution 2

There is no built-in fast-forward or rewind feature, but you can write a loop that seeks to currentPosition + Duration(seconds: 10) and then sleeps for 1 second, repeatedly. This is how it is implemented in the audio_service example, using a helper class similar to the following:

class Seeker {
  /// A reference to your just_audio player
  final AudioPlayer player;
  final Duration positionInterval;
  final Duration stepInterval;
  final Duration fileDuration;
  bool _running = false;

  Seeker(
    this.player,
    this.positionInterval,
    this.stepInterval,
    this.fileDuration,
  );

  start() async {
    _running = true;
    while (_running) {
      Duration newPosition = player.position + positionInterval;
      if (newPosition < Duration.zero) newPosition = Duration.zero;
      if (newPosition > fileDuration) newPosition = fileDuration;
      player.seek(newPosition);
      await Future.delayed(stepInterval);
    }
  }

  stop() {
    _running = false;
  }
}

To start fast-forwarding by jumping forward at 10 second intervals every 1 second, use:

      seeker = Seeker(player, Duration(seconds: 10),
          Duration(seconds: 1), mediaItem)
        ..start();

And stop fast-forwarding with:

seeker.stop();

To rewind, just pass in negative 10 seconds instead.

Share:
977
Str0b3
Author by

Str0b3

Updated on November 28, 2022

Comments

  • Str0b3
    Str0b3 over 1 year

    I'm using just_audio and it works great.

    But can't seem to find a way to fast-forward or rewind.

    Update: It turns out just_audio is part of the audio_service package, using audio_service solved the problem for me.

    • Chirag Chopra
      Chirag Chopra almost 4 years
      I am also using the same package but I am not able to find an audio streaming position and complete audio duration... can you please help?
    • Str0b3
      Str0b3 almost 4 years
      I think that you need to use audio_service
  • omer
    omer over 2 years
    aint working for me.