๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
D.evelop/MarkUp

[HTML]์˜ค๋””์˜ค ์žฌ์ƒ ์‹œ ์žฌ์ƒ ์ค‘์ธ ๋‹ค๋ฅธ ์˜ค๋””์˜ค ์ •์ง€ (HTML Audio DOM)

by Danne 2023. 12. 1.

 

๐Ÿ”Š  ์˜ค๋””์˜ค ์žฌ์ƒ

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)

 

 

HTMLAudioElement: Audio() constructor - Web APIs | MDN

The Audio() constructor creates and returns a new HTMLAudioElement which can be either attached to a document for the user to interact with and/or listen to, or can be used offscreen to manage and play audio.

developer.mozilla.org

 

audioObject.play()  : ์˜ค๋””์˜ค ์žฌ์ƒ ๋ฉ”์„œ๋“œ

audioObject.pause()  : ์˜ค๋””์˜ค ์ •์ง€ ๋ฉ”์„œ๋“œ

const playAudio = () => {
	track.play(); // ์˜ค๋””์˜ค ํ”Œ๋ ˆ์ด
	track.paused = false; // "์ •์ง€"์†์„ฑ ๊ฐ’ false
	iconAnimation("play"); // ํ”Œ๋ ˆ์ด ์ƒํƒœ๋กœ ์Šคํƒ€์ผ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜
}

const stopAudio = () => {
	track.pause(); // ์˜ค๋””์˜ค์ •์ง€
	track.currentTime = 0; // ์˜ค๋””์˜ค ์žฌ์ƒ์‹œ๊ฐ„์„ 0์œผ๋กœ ๋ณ€๊ฒฝ
	iconAnimation("stop"); // ์ •์ง€ ์ƒํƒœ๋กœ ์Šคํƒ€์ผ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€