Flutter audioplayers:如何逐个播放音频列表(在前一个音频结束时)

huangapple go评论66阅读模式
英文:

Flutter audioplayers: How can I play a list of audios one by one (when the previous ends)

问题

以下是您的代码的翻译部分:

  AudioPlayer? _player;
  void _play(number) async {
    _player?.dispose();
    final player = _player = AudioPlayer();
    print("尝试播放编号 $number");
    player.play(AssetSource('audio/$number.wav'));
  }
...
  void _playList(list) async{
    for (int i=0;i<list.length;i++){
      _play(i);
    }
  }

如果您有其他需要,请随时告诉我。

英文:

How can I play a list of (short) audio one by one using audioplayers?

I now have the following code:

  AudioPlayer? _player;
  void _play(number) async {
    _player?.dispose();
    final player = _player = AudioPlayer();
    print(&quot;trying to play number $number&quot;);
    player.play(AssetSource(&#39;audio/$number.wav&#39;));
  }
...
  void _playList(list) async{
    for (int i=0;i&lt;list.length;i++){
      _play(i);
    }
  }

But because I am creating multiple AudioPlayer objects here, the audios got played simultaneously.

What I want to achieve is: Play the first audio, wait until it reaches the end, then play the next audio. Loop until all the audios in the list got played.

答案1

得分: 1

好的,我将在这里回答自己的问题。

AudioPlayer player = AudioPlayer();
void playList(list) async{
player.play(AssetSource('audio/${list[0]}.wav'));
int i = 1;
player.onPlayerComplete.listen((
) {
if (i < list.length) {
player.play(AssetSource('audio/${list[i]}.wav'));
i++;
}
});
}


<details>
<summary>英文:</summary>

ok, I will answer my own question here.



AudioPlayer player= AudioPlayer();
void playList(list) async{
player.play(AssetSource('audio/${list[0]}.wav'));
int i =1;
player.onPlayerComplete.listen((
) {
if (i<list.length) {
player.play(AssetSource('audio/${list[i]}.wav'));
i++;
}
});
}


</details>



huangapple
  • 本文由 发表于 2023年5月11日 11:12:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定