我正在使用Flutter的Just-Audio插件来播放从我的应用程序中的streambuilder中获取的一个mp3文件。streambuilder返回setClip函数所需的文件的持续时间;
player.setClip(start: Duration(milliseconds: 0), end: Duration(milliseconds: 10);而不是"10",“结束”点应该是文件的持续时间减去500毫秒。所以我在我的initState中有了这个流监听器;
@override
void initState() {
super.initState();
_init();
}
Future<void> _init() async {
await player.setUrl('https://bucket.s3.amazonaws.com/example.mp3');
player.durationStream.listen((event) {
int newevent = event.inMilliseconds;
});
await player.setClip(start: Duration(milliseconds: 0), end: newevent);
}但我需要将获取的持续时间转换为整数,这样才能起飞500 in。不幸的是,int newevent = event.inMilliseconds;抛出以下错误;
A value of type 'int' can't be assigned to a variable of type 'Duration?'. Try changing the type of the variable, or casting the right-hand type to 'Duration?'.我试过这个
int? newevent = event?.inMilliseconds;然后;
await player.setClip(start: Duration(milliseconds: 0), end: Duration(milliseconds: newevent));但是在milliseconds: newevent下,我只得到了这个红线错误;
The argument type 'Duration?' can't be assigned to the parameter type 'int'.那么我如何从我的流监听器中获得作为整数的持续时间,以便在player.setClip中使用它作为结束点
发布于 2021-05-18 14:12:38
出现这个问题是因为durationStream返回一个可空持续时间,并且它必须是不可空的,才能将其转换为整数。可以使用null检查将持续时间提升到非空类型。
此外,要只在第一个事件之后运行setClip,请使用first而不是listen,并在函数中移动setClip:
player.durationStream.first.then((event) {
if(event != null){
int newevent = event.inMilliseconds - 500;
await player.setClip(start: Duration(milliseconds: 0), end: Duration(milliseconds: newevent);
}
});https://stackoverflow.com/questions/67587598
复制相似问题