๐ ์น์์ ์์ ๊ฐ์ฒด(Audio) ๋ค๋ฃจ๊ธฐ
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>
์ค๋์ค ํ์ผ ํฌ๋งท ์ง์
<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
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