我创建了播放器音频,并使用了just_audio插件,但音频播放器完成后无法返回到开头时出现了问题。我尝试使用player.playerStateStream.listen()
,但当我单击播放图标时,它无法播放音频,开始持续时间也不会更改为0,但当我手动移动滑块时,它可以工作,如何解决此问题?
编辑:
我需要什么?
我需要音频完成后,滑块回到开始和重置持续时间,就像我第一次播放音频和暂停与onPressed
播放音频的音频。
代码:
String currentTime = "", endTime = "";
double minDuration = 0, maxDuration = 0, currentDuration = 0;
void setAudio(String url) async {
setState(() => isLoadAudio = true);
await player.setUrl(url);
currentDuration = minDuration;
maxDuration = player.duration!.inMilliseconds.toDouble();
setState(() {
currentTime = getDuration(currentDuration);
endTime = getDuration(maxDuration);
});
isPlaying = false;
changeStatusPlaying();
player.positionStream.listen((duration) {
currentDuration = duration.inMilliseconds.toDouble();
setState(() => currentTime = getDuration(currentDuration));
});
setState(() => isLoadAudio = false);
player.playerStateStream.listen((state) {
if (state.processingState == ProcessingState.completed) {
setState(() {
currentDuration = minDuration;
if (isRepeating == true) {
isPlaying = true;
} else {
isPlaying = false;
isRepeating = false;
}
});
}
});
}
void changeStatusPlaying() {
setState(() => isPlaying = !isPlaying);
isPlaying ? player.play() : player.pause();
currentDuration == maxDuration ? isPlaying : !isPlaying;
}
String getDuration(double value) {
Duration duration = Duration(milliseconds: value.round());
return [duration.inHours, duration.inMinutes, duration.inSeconds]
.map((e) => e.remainder(60).toString().padLeft(2, "0"))
.join(":");
}
Slider(
value: currentDuration,
min: minDuration,
max: maxDuration,
onChanged: (v) {
currentDuration = v;
playAudio(currentDuration);
},
),
2条答案
按热度按时间cbeh67ev1#
将音频播放器设置为循环播放:
完整示例(将音频文件放在/assets/audio):
j9per5c42#