Expo 显示音频的剩余秒数

问题描述

我想显示音频还剩多少秒。像这样:

enter image description here

我知道我需要使用 positionMillisdurationMillis 道具,但我如何监控 positionMillis

当我点击一个按钮时,我会加载音频并播放它(handleAudio 函数)。这是我只会得到 positionMillis 一次(或者如果我暂停/恢复音频)。 如何持续观看音频的 positionMillis 道具?

const [sound,setSound] = useState(null);
const [soundStatus,setSoundStatus] = useState({ status: null,icon: play });

  async function handleAudio() {
    //playing audio for the first time
    if (soundStatus.status === null) {
      console.log("Loading Sound");
      const { sound,status } = await Audio.sound.createAsync(
        {
          uri: `some_url`,},{ shouldplay: true },);
      setSound(sound);
      setSoundStatus({ status: status,icon: pause });
    }

    //pause audio
    if (soundStatus.status) {
      if (soundStatus.status.isLoaded && soundStatus.status.isPlaying) {
        const status = await sound.pauseAsync();
        console.log("pausing audio");
        setSoundStatus({ status: status,icon: play });
      }

      //resuming audio
      if (soundStatus.status.isLoaded && !soundStatus.status.isPlaying) {
        const status = await sound.playAsync();
        console.log("resuming audio");
        setSoundStatus({ status: status,icon: pause });
      }
    }
  }

<Button title="Play sound" onPress={handleAudio}/>
<Text> `${soundstatus.status.positionMillis}:${soundstatus.status.durationMillis}` </Text>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...