我试着从音频标签中获取持续时间。我的代码通过axios从服务器中获取音频文件,并将其置于音频标签上。
//Parents Component const [sound, setSound] = useState({ url: "", timestamp: {}, }); ... //Child get sound from Parents Component by props. //That's worked well, and playing file worked well too. {/* Playing sound Tag */} <audio ref={audioRef} src={sound.url} onLoadedData={loadedData} onTimeUpdate={getCurrentDuration} preload="metadata" ></audio>
和这里的问题。
//#region 진행 시간, 퍼센트 구하는 함수 const getCurrentDuration = (e) => { if (e.currentTarget) { const currentTime = e.currentTarget.currentTime; /* Return current audio bar's length to seconds -> This point occurs problem. Duration return to NaN NaN occurs when there is no data, but when I checked it with the console Even though it is executed after carrying sound, NaN occurs */ const duration = e.currentTarget.duration; const percent = ((currentTime / duration) * 100).toFixed(2); console.log(e.currentTarget) setPercentage(+percent); setCurrectTime(currentTime.toFixed(2)); } };