问题描述
我想显示音频还剩多少秒。像这样:
我知道我需要使用 positionMillis
和durationMillis
道具,但我如何监控 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 (将#修改为@)