英文:
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("trying to play number $number");
player.play(AssetSource('audio/$number.wav'));
}
...
void _playList(list) async{
for (int i=0;i<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论