D.evelop/MarkUp
[HTML]μ€λμ€ μ¬μ μ μ¬μ μ€μΈ λ€λ₯Έ μ€λμ€ μ μ§ (HTML Audio DOM)
Danne
2023. 12. 1. 19:21
π μ€λμ€ μ¬μ
let track = new Audio();
let flag = 0; // flagλ₯Ό μ¬μ©νμ¬ μ¬μ μ€μΈ μ€λμ€ μμΌλ©΄ μ μ§
const playVoice = (audioSrc) =>{
track.src = audioSrc;
console.log(track.paused)
if(flag === 0){
flag = 1;
playAudio();
}else{
flag = 0;
stopAudio()
}
}
const playAudio = () => {
track.play();
track.paused = false
iconAnimation("play");
}
const stopAudio = () => {
track.pause();
track.currentTime = 0;
iconAnimation("stop");
}
track.addEventListener('ended', function() {
iconAnimation();
});
const iconVoice = document.querySelectorAll('.icon_voice');
const iconAnimation = (status) =>{
iconVoice.forEach(function(icon){
if(status === "play"){
icon.classList.add('play');
}else{
icon.classList.remove('play');
}
})
}
new Audio(filePath) : μ€λμ€ μμ±μ
new Audio(url)
audioObject.play() : μ€λμ€ μ¬μ λ©μλ
audioObject.pause() : μ€λμ€ μ μ§ λ©μλ
const playAudio = () => {
track.play(); // μ€λμ€ νλ μ΄
track.paused = false; // "μ μ§"μμ± κ° false
iconAnimation("play"); // νλ μ΄ μνλ‘ μ€νμΌ λ³κ²½νλ ν¨μ
}
const stopAudio = () => {
track.pause(); // μ€λμ€μ μ§
track.currentTime = 0; // μ€λμ€ μ¬μμκ°μ 0μΌλ‘ λ³κ²½
iconAnimation("stop"); // μ μ§ μνλ‘ μ€νμΌ λ³κ²½νλ ν¨μ
}
λ°μν