Language/JavaScript (WEB)

๐Ÿ”Š ์›น์—์„œ ์Œ์•… ๊ฐ์ฒด(Audio) ๋‹ค๋ฃจ๊ธฐ

์ธํŒŒ_ 2023. 4. 1. 20:18

์Œ์•…-๊ฐ์ฒด

HTML Audio ํƒœ๊ทธ

HTML <audio> ํƒœ๊ทธ๋Š” ์›น ํŽ˜์ด์ง€์—์„œ ์˜ค๋””์˜ค๋ฅผ ์žฌ์ƒํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์š”์†Œ์ด๋‹ค. MP3, WAV, OGG ๋“ฑ ๋‹ค์–‘ํ•œ ์˜ค๋””์˜ค ํฌ๋งท ํŒŒ์ผ์„ ์›น์—์„œ ์žฌ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

audio ํƒœ๊ทธ ์†์„ฑ

์†์„ฑ ๊ฐ’ ์„ค์ • ๋‚ด์šฉ
src ํŒŒ์ผ ๊ฒฝ๋กœ ์žฌ์ƒํ•  ์Œ์› ํŒŒ์ผ์˜ ๊ฒฝ๋กœ ์„ค์ •(mp3, wav, ogg ํŒŒ์ผ ๋“ฑ)
autoplay autoplay ์ž๋™ ์žฌ์ƒ ์—ฌ๋ถ€ ์„ค์ •(๋ธŒ๋ผ์šฐ์ €์— htmlํŒŒ์ผ์ด ๋กœ๋“œ๋˜์ž๋งˆ์ž ์Œ์•…ํŒŒ์ผ์ด ์žฌ์ƒ๋จ)
 loop loop ๋ฐ˜๋ณต ์žฌ์ƒ ์—ฌ๋ถ€ ์„ค์ •
controls controls ์ปจํŠธ๋กค ํŒจ๋„(์žฌ์ƒ/์ •์ง€ ๋ฒ„ํŠผ ๋“ฑ) ๋…ธ์ถœ ์—ฌ๋ถ€ ์„ค์ •
muted muted ์Œ์†Œ๊ฑฐ ์„ค์ •
<!-- src ์†์„ฑ์„ ํ†ตํ•ด ์˜ค๋””์˜ค ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ • -->
<!-- controls ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์˜ค๋””์˜ค ์žฌ์ƒ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ํ‘œ์‹œ -->
<!-- autoplay ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋  ๋•Œ ์ž๋™์œผ๋กœ ์˜ค๋””์˜ค๊ฐ€ ์žฌ์ƒ -->
<!-- loop ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์˜ค๋””์˜ค๊ฐ€ ๋๋‚˜๋ฉด ์ž๋™์œผ๋กœ ๋ฐ˜๋ณต ์žฌ์ƒ -->
<audio src="sound.mp3" autoplay loop controls id="myAudio"></audio>

HTML Audio ํƒœ๊ทธ

 

์˜ค๋””์˜ค ํŒŒ์ผ ํฌ๋งท ์ง€์ •

<audio> ํƒœ๊ทธ ๋‚ด๋ถ€์— <source> ํƒœ๊ทธ๋ฅผ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ํƒ€์ž…์˜ ์˜ค๋””์˜ค ํฌ๋งท์„ ์ž…๋ ฅํ•ด์ฃผ๋ฉด, ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์•Œ์•„์„œ ์ง€์›ํ•˜๋Š” ํฌ๋งท์œผ๋กœ ์žฌ์ƒํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  type ์†์„ฑ์—๋Š” ํ•ด๋‹น ์˜ค๋””์˜ค ํŒŒ์ผ์˜ MIME ํƒ€์ž…์„ ์ง€์ •ํ•œ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ <source> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ง€์›ํ•˜๋Š” ์˜ค๋””์˜ค ํŒŒ์ผ ํƒ€์ž…์„ ์šฐ์„ ์ ์œผ๋กœ ์žฌ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

<audio autoplay loop controls>
  <source src="sound.mp3" type="audio/mpeg">
  <source src="sound.ogg" type="audio/ogg">
  ์˜ค๋””์˜ค๊ฐ€ ์ง€์›๋˜์ง€ ์•Š๋Š” ๋ธŒ๋ผ์šฐ์ €์ž…๋‹ˆ๋‹ค. <!-- <audio> ์š”์†Œ ๋‚ด์— ์œ„์น˜ํ•˜๋Š” ํ…์ŠคํŠธ๋Š” ์‚ฌ์šฉ์ž์˜ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ <audio> ์š”์†Œ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ํ™”๋ฉด์— ํ‘œ์‹œ -->
</audio>
๋ธŒ๋ผ์šฐ์ € ์ง€์› ํฌ๋งท
Internet Explorer MP3, WAV
Chrome MP3, WAV, Ogg Vorbis
Firefox MP3, WAV, Ogg Vorbis
Safari MP3, WAV, AAC

Javascript๋กœ Audio ๋‹ค๋ฃจ๊ธฐ

์œ„์˜ ์˜ค๋””์˜ค ์š”์†Œ๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ƒ์—์„œ ์žฌ์ƒ/์ผ์‹œ ์ •์ง€ ๋“ฑ์˜ ์„ธ๋ฐ€ํ•œ ์ปจํŠธ๋กค์„ ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ƒ์—์„œ Audio ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜, ํ˜น์€ HTML ์ƒ์— <audio> ํƒœ๊ทธ๋ฅผ ์„ ์–ธํ•ด๋‘” ์ƒํƒœ๋ผ๋ฉด, ์—˜๋ฆฌ๋จผํŠธ ์ทจ๋“ ๋ฐฉ์‹์œผ๋กœ audio ๊ฐ์ฒด๋ฅผ ํ™•๋ณดํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

const audio = new Audio('audio.mp3'); // `audio.mp3` ํŒŒ์ผ์„ ๋กœ๋”ฉ
audio.play(); // ์˜ค๋””์˜ค ์žฌ์ƒ
<!-- HTML audio ํƒœ๊ทธ -->
<audio src="sound.mp3" id="myAudio"></audio>

<script>
    const myAudio = document.getElementById("myAudio") // Audio๊ฐ์ฒด ์ทจ๋“
    myAudio.play(); // ์Œ์› ์žฌ์ƒ
    myAudio.pause(); // ์ผ์‹œ ์ •์ง€
</script>

์ด๋Ÿฌํ•œ  Audio ๊ฐ์ฒด๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ์™€ ์ด๋ฒคํŠธ๋ฅผ ์ œ๊ณตํ•˜๋ฏ€๋กœ, ๋‹ค์–‘ํ•œ ์˜ค๋””์˜ค ์ œ์–ด ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

See the Pen MP3 Playback via HTML5 Audio Elements by Anson Kao (@ansonkao) on CodePen.


Audio ๊ฐ์ฒด

 

Audio ๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ

ํ”„๋กœํผํ‹ฐ ์„ค์ • ๋‚ด์šฉ
src ์Œ์›ํŒŒ์ผ ๊ฒฝ๋กœ : "๊ฒฝ๋กœ/ํŒŒ์ผ๋ช….ํ™•์žฅ์ž"
volume ๋ณผ๋ฅจ : 0~1
loop ๋ฐ˜๋ณต ์—ฌ๋ถ€: true | false
autoplay ์ž๋™์žฌ์ƒ ์—ฌ๋ถ€: true | false
muted ์Œ์†Œ๊ฑฐ ์—ฌ๋ถ€: true | false
paused ์ผ์‹œ์ •์ง€ ์—ฌ๋ถ€: true | false
ended ์žฌ์ƒ์™„๋ฃŒ ์—ฌ๋ถ€: true | false
duration ์Œ์›์˜ ์ „์ฒด ๊ธธ์ด(์ดˆ ๋‹จ์œ„)
currentTime ์Œ์›์˜ ํ˜„์žฌ ์žฌ์ƒ ์œ„์น˜(์ดˆ ๋‹จ์œ„)
const audio = new Audio('audio.mp3');

// audio๊ฐ€ load ๋  ๋•Œ ์ž๋™์žฌ์ƒ ๋จ
audio.autoplay = true;

// audio์˜ ์žฌ์ƒ์‹œ์ ์„ 5์ดˆ๋กœ ์„ค์ •ํ•จ
audio.currentTime = 5;

// audio์˜ ๊ธธ์ด๋ฅผ ์ดˆ(seconds) ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜
audio.duration;

// audio๋ฅผ ๋ฐ˜๋ณต ์žฌ์ƒํ•จ
audio.loop = true;

// audio ์Œ์†Œ๊ฑฐ
audio.muted = true;

// audio์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•จ(URL)
audio.src = "my_audio.mp3";

// audio์˜ ์Œ๋Ÿ‰์„ 0.2๋กœ ์ง€์ •ํ•จ
// ์Œ๋Ÿ‰์€ 0.0 ~ 1.0 ์‚ฌ์ด ๊ฐ’์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ , 1.0์ด ๊ฐ€์žฅ ํฐ ์Œ๋Ÿ‰
audio.volume = 0.2;

 

Audio ๊ฐ์ฒด ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ ์ž‘๋™ ๋‚ด์šฉ
play() ์žฌ์ƒ 
pause() ์ผ์‹œ์ •์ง€
addTextTrack() ์ƒˆ๋กœ์šด ํŠธ๋ž™์„ ์ถ”๊ฐ€
canPlayType() ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํ•ด๋‹น ์˜ค๋””์˜ค ํƒ€์ž…์„ ์žฌ์ƒํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์ฒดํฌ
load() ์˜ค๋””์˜ค ๊ฐ์ฒด๋ฅผ ๋ฆฌ๋กœ๋“œ
const audio = new Audio('audio.mp3');

audio2.loop = true; // ๋ฐ˜๋ณต์žฌ์ƒํ•˜์ง€ ์•Š์Œ
audio2.volume = 0.5; // ์Œ๋Ÿ‰ ์„ค์ •

audio2.play(); // sound2.mp3 ์žฌ์ƒ

setTimeout(function () { // 5์ดˆ ํ›„ sound2.mp3 ์ผ์‹œ์ •์ง€
	audio2.pause();
}, 5000);

// audio๋ฅผ ๋‹ค์‹œ loadํ•จ. ์ฃผ๋กœ audio์˜ src๋‚˜ ์„ค์ •์ด ๋ฐ”๋€Œ์—ˆ์„ ๊ฒฝ์šฐ์— ์‚ฌ์šฉ
audio.src = "sound2.mp3";
audio.load();
/* ์˜ค๋””์˜ค ์ฒ˜์Œ๋ถ€ํ„ฐ ์žฌ์ƒ */
audio.currentTime = 0; // ์žฌ์ƒ ์œ„์น˜๋ฅผ ์ฒ˜์Œ์œผ๋กœ ์„ค์ •
audio.play(); // ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์žฌ์ƒ๋จ

 

Audio ๊ฐ์ฒด ์ด๋ฒคํŠธ

์˜ค๋””์˜ค๊ฐ€ ์žฌ์ƒ๋˜๊ฑฐ๋‚˜ ์ผ์‹œ์ •์ง€๋  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ ๋“ฑ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. 

์ด๋ฒคํŠธ ์ƒํ™ฉ
canplaythrough ์Œ์› ํŒŒ์ผ์ด ๋ชจ๋“œ ๋กœ๋“œ๋˜์–ด ์žฌ์ƒ ๊ฐ€๋Šฅํ•  ๋•Œ
play ์žฌ์ƒ์ด ์‹œ์ž‘๋  ๋•Œ
playing ์žฌ์ƒ ์ค‘์ผ ๋•Œ
puase ์ผ์‹œ ์ •์ง€๋˜์—ˆ์„ ๋•Œ
ended ์žฌ์ƒ์ด ์™„๋ฃŒ๋˜์—ˆ์„ ๋•Œ
volumechange ๋ณผ๋ฅจ์ด ๋ณ€๊ฒฝ๋  ๋•Œ
// ์˜ค๋””์˜ค ์žฌ์ƒ ์‹œ
audio.addEventListener('play', function() {
  console.log('์˜ค๋””์˜ค๊ฐ€ ์žฌ์ƒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
});

// ์˜ค๋””์˜ค ์ผ์‹œ์ •์ง€ ์‹œ
audio.addEventListener('pause', function() {
  console.log('์˜ค๋””์˜ค๊ฐ€ ์ผ์‹œ์ •์ง€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
});

์Œ์› ์ž๋™ ์žฌ์ƒ ๋ธŒ๋ผ์šฐ์ € ์ •์ฑ…

ํฌ๋กฌ์ด๋‚˜ ์‚ฌํŒŒ๋ฆฌ, ํŒŒ์ด์–ดํญ์Šค ๊ฐ™์€ ๋Œ€๋ถ€๋ถ„ ๋ธŒ๋ผ์šฐ์ €์˜ ๊ฒฝ์šฐ ๋ณด์•ˆ ์ •์ฑ… ์ƒ ์‚ฌ์šฉ์ž์˜ ๋ช…์‹œ์ ์ธ ์•ก์…˜(ํด๋ฆญ ..๋“ฑ)์ด ์—†๋Š” ์ƒํƒœ์—์„œ์˜ ์ž๋™ ์Œ์› ์žฌ์ƒ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์€ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์˜ค๋””์˜ค ๊ฐ์ฒด๋ฅผ ๋กœ๋“œํ•˜๊ณ  ์ž๋™์œผ๋กœ play() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์Œ์›์„ ์žฌ์ƒํ•˜๋„๋ก ์„ค์ •ํ•˜์˜€์ง€๋งŒ ๋ธŒ๋ผ์šฐ์ € ์ƒ์—์„œ ์ด๋ฅผ ์ฐจ๋‹จํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

const audio = new Audio('audio.mp3');
audio.play();

์ฆ‰, ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ„ํŠผ ํด๋ฆญ๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ํ†ตํ•ด play() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผ๋งŒ ์Œ์›์„ ์žฌ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ•ด์•ผ ํ•œ๋‹ค.

<button id="play-button">์Œ์•… ์žฌ์ƒ</button>

<script>
    const audio = new Audio('audio.mp3');
    const playButton = document.getElementById('play-button');

    playButton.addEventListener('click', function() {
      audio.play();
    });
</script>

์ด์ „์—๋Š”, Chrome ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์Œ์›์„ ์ž๋™์œผ๋กœ ์žฌ์ƒํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ–ˆ์ง€๋งŒ, ์ด๋Š” ์•…์„ฑ์ฝ”๋“œ์˜ ์œ ํฌ๋‚˜ ์‚ฌ์šฉ์ž์˜ ๊ฐœ์ธ์ •๋ณด๋ฅผ ํƒˆ์ทจํ•˜๋Š” ๋“ฑ์˜ ๋ณด์•ˆ ์œ„ํ˜‘์ด ๋  ์ˆ˜ ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.


Audio Player ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

๋งŒ์ผ ์›นํŽ˜์ด์ง€์— ๋ฉ‹์ง„ mp3 ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด์ง„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๋ฐฉ๋ฒ•์ด๋‹ค. ์ž์ฒด์ ์œผ๋กœ CSS ์Šคํƒ€์ผ๋ง์ด ๋˜์–ด ์žˆ์–ด์„œ ๋Š๋‚Œ ์ข‹์€ ๋ฏธ๋””์–ด ํ”Œ๋ ˆ์ด์–ด๋ฅผ ์ž์‹ ์˜ ํ™ˆํŽ˜์ด์ง€์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Calamansi.js - HTML5 & JS Audio Player by Voerro | Voerro Tutorials

Live demo of Calamansi.js - a HTML5 & JS Audio Player by Voerro

www.voerro.com

Audio Player


 

OnAirCode

We’ll provide resources, examples, tutorials related to web design, graphics, mobile app development for designer and developer with ideas.

onaircode.com

 

35+ JavaScript Music Players - Free Code + Demos

1. Music Player App Author: Rafaela Lucas (rafaelavlucas) Created on: December 26, 2019 Made with: HTML, SCSS, JS Tags: music-player, music stream, player songs, daily ui, daily ui challenge 2. HTML Audio Player Author: Zakari Abdessamad (vanderzak) Create

devsnap.me

 

26 HTML Audio Player For Websites In Codepen

If you have a music related website and want to improve the design or add more functions capabilities for your music player to attract as…

niemvuilaptrinh.medium.com


Web Audio API

์ด์ฒ˜๋Ÿผ HTML์—์„œ audio ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜๋ฉด, ์˜ค๋””์˜ค๋ฅผ ์žฌ์ƒ/์ •์ง€ํ•˜๊ณ  ๋ณผ๋ฅจ์„ ์กฐ์ •ํ•˜๊ฑฐ๋‚˜ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋”ฑ ์ด ์ •๋„์˜ ๊ธฐ๋Šฅ์ด ๋์ด๋‹ค. ๊ทธ ์ด์ƒ์€ ์—†๋‹ค.

Web Audio API๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์˜ค๋””์˜ค ์ฒ˜๋ฆฌ์™€ ์žฌ์ƒ์„ ์œ„ํ•œ ๊ฐ•๋ ฅํ•œ ์‹ ์ƒ API ์ด๋‹ค. ์˜ค๋””์˜ค ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ˜ํ”Œ๋งํ•˜๊ณ , ํ•„ํ„ฐ๋งํ•˜๊ณ , ์กฐ์ž‘ํ•˜์—ฌ ์˜ค๋””์˜ค ์Œ์› ์ž์ฒด๋ฅผ ์ปจํŠธ๋กคํ•˜๊ณ  ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ด์ฃผ์–ด ์ผ๋ฐ˜ Audio ๊ฐ์ฒด๋ณด๋‹ค ๋” ๋†’์€ ์ˆ˜์ค€์˜ ์˜ค๋””์˜ค ์ œ์–ด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์  ๋“ฑ์ด ์žˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ์Œ์›์— ๋‹ค์–‘ํ•œ ์ดํŽ™ํŠธ ํšจ๊ณผ๋ฅผ ์ค„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์Œ์›์˜ ์ฃผํŒŒ์ˆ˜ ๋ฐ์ดํ„ฐ ์ทจ๋“์„ ํ†ตํ•œ Pitch(๋†’๋‚ฎ์ด), Gain(๋ณผ๋ฅจ) ๋“ฑ์˜ ๊ณ„์‚ฐ๋„ ๊ฐ€๋Šฅํ•˜๊ณ  ๋งˆ์ดํฌ๋ฅผ ํ†ตํ•ด ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ์†Œ๋ฆฌ์— ๋Œ€ํ•œ ๋ถ„์„๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

Web Audio API - Web API | MDN

Web Audio API๋Š” ์›น์—์„œ ์˜ค๋””์˜ค๋ฅผ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ•๋ ฅํ•˜๊ณ  ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Web Audio API๋ฅผ ์ด์šฉํ•˜๋ฉด ์˜ค๋””์˜ค ์†Œ์Šค๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ฑฐ๋‚˜, ์˜ค๋””์˜ค์— ์ดํŽ™ํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜, ์˜ค๋””์˜ค๋ฅผ ์‹œ

developer.mozilla.org

<audio src="audio.mp3"></audio>
<label for="playback-rate">Playback Rate</label>
<input type="range" id="playback-rate" min="0.5" max="2" step="0.1" value="1">

<script>
    // ์˜ค๋””์˜ค ํŒŒ์ผ ๋กœ๋“œ
    const audioContext = new AudioContext(); // ์˜ค๋””์˜ค ์ปจํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑ
    const audioElement = document.querySelector('audio'); // HTML audio ์š”์†Œ ๊ฐ€์ ธ์˜ค๊ธฐ
    const audioSource = audioContext.createMediaElementSource(audioElement); // ์˜ค๋””์˜ค ์†Œ์Šค๋ฅผ ์ƒ์„ฑ

    // ์žฌ์ƒ ์†๋„ ๋ณ€๊ฒฝ์„ ์œ„ํ•œ ์Šฌ๋ผ์ด๋” ์ƒ์„ฑ
    const playbackRateSlider = document.querySelector('#playback-rate');
    playbackRateSlider.addEventListener('input', () => {
      // ์‚ฌ์šฉ์ž๊ฐ€ ์Šฌ๋ผ์ด๋”๋ฅผ ์กฐ์ž‘ํ•˜๋ฉด playbackRate ์†์„ฑ(์˜ค๋””์˜ค ์žฌ์ƒ ์†๋„)๊ฐ€ ๋ณ€๊ฒฝ
      audioElement.playbackRate = playbackRateSlider.value;
    });

    // ์˜ค๋””์˜ค ์žฌ์ƒ
    audioSource.connect(audioContext.destination); // ์˜ค๋””์˜ค ์†Œ์Šค๋ฅผ ์˜ค๋””์˜ค ์ปจํ…์ŠคํŠธ์˜ ์ถœ๋ ฅ์— ์—ฐ๊ฒฐ
    audioElement.play();
</script>

๊ทธ๋Ÿฌ๋‚˜ Web Audio API๋Š” ๊ฐ•๋ ฅํ•œ ๋งŒํผ ์ผ๋ฐ˜ Audio ๊ฐ์ฒด๋ณด๋‹ค ๋ณต์žกํ•ด ๋†’์€ ํ•™์Šต๋ฅ ์„ ์š”๊ตฌํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹จ์ˆœํ•œ ์˜ค๋””์˜ค ์žฌ์ƒ ๊ธฐ๋Šฅ๋งŒ์„ ํ•„์š”๋กœ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ผ๋ฐ˜ Audio ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ ํ•ฉํ•  ์ˆ˜ ์žˆ๊ณ , ๋‹ค์–‘ํ•œ ์‚ฌ์šด๋“œ ์ž‘์—… ๋ฐ ๋ถ„์„์ด ํ•„์š”ํ•˜๋‹ค๋ฉด Web Audio API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.


# ์ฐธ๊ณ ์ž๋ฃŒ

http://www.tcpschool.com/html-tags/audio 

https://stackoverflow.com/questions/9419263/how-to-play-audio

https://curryyou.tistory.com/337