0

我正在使用just_audio,效果很好。

但似乎找不到快进或倒带的方法。

更新:原来 just_audio 是audio_service包的一部分,使用 audio_service 为我解决了这个问题。

4

2 回答 2

2

player.seek(Duration(seconds: player.position.inSeconds - 15));快退 15 秒 player.seek(Duration(seconds: player.position.inSeconds + 15));前进 15 秒'

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

player来自_AudioPlayer player

于 2021-04-30T20:57:54.040 回答
2

没有内置的快进或快退功能,但您可以编写一个循环currentPosition + Duration(seconds: 10),反复寻找然后休眠 1 秒。这是在 audio_service 示例中的实现方式,使用类似于以下的帮助器类:

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;
  }
}

要通过每 1 秒以 10 秒间隔向前跳来开始快进,请使用:

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

并停止快进:

seeker.stop();

要倒带,只需传入负 10 秒即可。

于 2020-10-02T16:04:26.280 回答