728x90
1. audio를 생성한다.
const tempSound = new Audio('../include/media/temp.mp3');
2. audio의 duration을 초깃값을 0을 넣어 변수로 선언한다.
let tempSoundDuration = 0;
3. onloadedata를 통해 audio의 daration을 가져온다.
tempSound.onloadeddata = () => {
tempSoundDuration = tempSound .duration;
};
위와 같이 onloadedata를 사용하지 않고 tempSound.duration만 찍어봤을 때는 NaN이 나올 것이다.
데이터가 갱신되기 전이기 때문이다. 따라서 load된 뒤 가져오기 위해 onloadedata를 사용한다.
4. console.log를 통해 찍어본다. 잘나온다.
console.log(tempSoundDuration );
'개발일지 > Web Development' 카테고리의 다른 글
이터러블 사용(for 루프 / forEach / for-of) (0) | 2024.04.09 |
---|---|
피셔 예이츠 셔플 Fisher Yates Shuffle 알고리즘(배열 섞기) (0) | 2023.11.09 |
[CSS] 스타일 시트 CSS 브라우저 별 접두어 (0) | 2022.11.29 |
[JavaScript] JavaScript yyyy-mm.dd 4자리 년도를 2자리 년도로 format 하는 방법 (0) | 2022.10.13 |
[HTML] input type="password" 안 보일 때 해결법! (0) | 2022.09.27 |