首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只是音频颤振插件-如何将持续时间从流侦听器转换为整数?

只是音频颤振插件-如何将持续时间从流侦听器转换为整数?
EN

Stack Overflow用户
提问于 2021-05-18 13:50:34
回答 1查看 364关注 0票数 0

我正在使用Flutter的Just-Audio插件来播放从我的应用程序中的streambuilder中获取的一个mp3文件。streambuilder返回setClip函数所需的文件的持续时间;

代码语言:javascript
复制
player.setClip(start: Duration(milliseconds: 0), end: Duration(milliseconds: 10);

而不是"10",“结束”点应该是文件的持续时间减去500毫秒。所以我在我的initState中有了这个流监听器;

代码语言:javascript
复制
@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;抛出以下错误;

代码语言:javascript
复制
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?'.

我试过这个

代码语言:javascript
复制
 int? newevent = event?.inMilliseconds;

然后;

代码语言:javascript
复制
 await player.setClip(start: Duration(milliseconds: 0), end: Duration(milliseconds: newevent));

但是在milliseconds: newevent下,我只得到了这个红线错误;

代码语言:javascript
复制
 The argument type 'Duration?' can't be assigned to the parameter type 'int'.

那么我如何从我的流监听器中获得作为整数的持续时间,以便在player.setClip中使用它作为结束点

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-18 14:12:38

出现这个问题是因为durationStream返回一个可空持续时间,并且它必须是不可空的,才能将其转换为整数。可以使用null检查将持续时间提升到非空类型。

此外,要只在第一个事件之后运行setClip,请使用first而不是listen,并在函数中移动setClip

代码语言:javascript
复制
player.durationStream.first.then((event) {
  if(event != null){
    int newevent = event.inMilliseconds - 500;
    await player.setClip(start: Duration(milliseconds: 0), end: Duration(milliseconds: newevent);
  }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67587598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档