๐Ÿ’ป์šฉ๋‡ฝ ๊ฐœ๋ฐœ ๋…ธํŠธ๐Ÿ’ป
article thumbnail
๋ฐ˜์‘ํ˜•

[JavaScript] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฒ„ํŠผ ํด๋ฆญ์œผ๋กœ audio ์ œ์–ด ํ•˜๊ธฐ

๐Ÿ“– ๋“ค์–ด๊ฐ€๋ฉฐ

์•ˆ๋…•ํ•˜์„ธ์š”. ์˜ค๋Š˜์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฒ„ํŠผ ํด๋ฆญ์œผ๋กœ ์˜ค๋””์˜ค๋ฅผ ์ œ์–ดํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋ฒ„ํŠผ ํด๋ฆญ์œผ๋กœ ์Œ์•…์„ ์ผœ๊ณ  ๋„๊ณ , ๋‹ค์Œ ์Œ์•…์œผ๋กœ ๋„˜๊ธฐ๋Š” ์—ฌ๋Ÿฌ ์Œ์•…์„ ์ œ์–ดํ•˜๋Š”  ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋ฉด์„œ HTML์—๋Š” audioํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์˜ค์ง ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋งŒ audio ์ œ์–ด๋ฅผ ํ–ˆ๋‹ค๊ฐ€ ๋ง‰ํ˜”์ง€๋งŒ, ๊ฒฐ๊ตญ ์ž˜ ํ•ด๊ฒฐ์„ ํ•ด์„œ ๋”ฐ๋กœ ์ •๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์•„์ฃผ ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ๋“ค์–ด์„œ,

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

HTML์— ๋”ฐ๋กœ ์˜ค๋””์˜ค ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  ์ด๋Ÿฐ ์‹์œผ๋กœ  audio ์ฝ”๋”ฉ์„ ํ•ด์„œ ๋กœ์ปฌ์—์„œ ์ž˜ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜๊ณ ,

๊นƒํ—ˆ๋ธŒ ํผ๋ธ”๋ฆฌ์‹ฑ์„ ํ•˜์—ฌ ์‚ฌ์ดํŠธ๋กœ ๋ฐฐํฌํ•˜๋‹ˆ๊น ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๋กœ ์˜ค๋””์˜ค ์žฌ์ƒ์ด ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.

 

Uncaugth (in promise) DOMException: Failed to load because no supported source was found.

DOMExeption ์˜ค๋ฅ˜

๊ทธ๋ž˜์„œ ๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ์•Œ์•„๋ณธ ๊ฒฐ๊ณผ

stackoverflow.com/questions/10792163/change-audio-src-with-javascript

 

change

I have multiple audio files that I want to stream based on the user selects. How do I do that? This is what I have so far and it doesn't seem to work. *UPDATE: Made a few changes and now its clai...

stackoverflow.com

์œ„์˜ ๋งํฌ์— ๋‚˜์™€์žˆ๋Š” ํ•ด๊ฒฐ์ฑ…์„ ์‘์šฉํ•ด์„œ HTML์— <audio> ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  <audio> ํƒœ๊ทธ ์•ˆ์— <source > ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ <source> ํƒœ๊ทธ์˜ src ์†์„ฑ์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ณ€๊ฒฝํ•ด ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์ด์ œ ๋ฒ„ํŠผ์„ ํด๋ฆญ์œผ๋กœ audio๋ฅผ ์ผœ๊ณ  ๋„๊ณ , ๋‹ค์Œ audio๋กœ ๋„˜๊ธฐ๋Š” ๋ฐฉ๋ฒ•๊นŒ์ง€ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

javascript audio ์ œ์–ด example ํ™”๋ฉด ๊ตฌ์„ฑ

1. ํŒŒ์ผ ๊ตฌ์„ฑ

๋จผ์ € ํŒŒ์ผ ๊ตฌ์„ฑ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

audio play ์˜ˆ์ œ ํŒŒ์ผ ๊ตฌ์„ฑ

2. HTML 

HTML ๊ตฌ์„ฑ์ž…๋‹ˆ๋‹ค.

<html>
<head>
  <title>audio play example</title>
<link rel="stylesheet" href="style.css">
</head>
<body>

  <audio id="audioContainer"> <!-- audio play๋ฅผ ์œ„ํ•จ -->
        <source id="audioSource" src="" /> <!-- audio ํŒŒ์ผ -->
        Your browser does not support the audio format.
      </audio>
      
      <div class="js-onOffWrap">
        <button class="js-playBtn">โ™ฌ Play Music</button>
        <button class="js-stopBtn">โ™ฌ Stop Music</button>
      </div>      
       <div class="js-audioNextBtn">โฏ</div>
       
<script src="script.js"></script>
</body>
</html>

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ audio play๋ฅผ ์œ„ํ•œ  <audio> ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  <audio> ํƒœ๊ทธ ์•ˆ์— <source> ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•จ์œผ๋กœ์จ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ <source> ํƒœ๊ทธ์˜ src ์†์„ฑ์„ ๋ณ€๊ฒฝํ•ด์คŒ์œผ๋กœ audio๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ ์™ธ์—๋Š” audio ์žฌ์ƒ๊ณผ, ์ •์ง€, ๋‹ค์Œ audio๋กœ ๋„˜๊ธฐ๋Š” ๋ฒ„ํŠผ๋“ค์ž…๋‹ˆ๋‹ค.

3. JavaScript

๋จผ์ € ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ „์ฒด์ ์ธ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

๋ถ€๋ถ„ ๋ถ€๋ถ„ ์ฝ”๋“œ๋ฅผ ํ•ด์„ํ•ด ๊ฐ€๋ฉด์„œ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

const audioContainer = document.querySelector('#audioContainer');
const playBtn = document.querySelector('.js-playBtn');
const stopBtn = document.querySelector('.js-stopBtn')
const audioNextBtn = document.querySelector('.js-audioNextBtn');

const MUSIC_COUNT = 3; // ์Œ์•… ๊ฐฏ์ˆ˜

let currentAudio = 1; // ํ˜„์žฌ ์Œ์•…


function playAudio() {
  audioContainer.volume = 0.2;
  audioContainer.loop = true;
  audioContainer.play();  
}

function stopAudio() {
  audioContainer.pause();  
}


function loadAudio() {
  const source = document.querySelector('#audioSource');
  source.src = `audio/${currentAudio}.mp3`;
  audioContainer.load();
  playAudio();
}

function handleNextButtonClick() { 

  if (currentAudio < MUSIC_COUNT) {
    currentAudio += 1;
  } else {
    currentAudio = 1;
  }
  
  audioContainer.pause();
  loadAudio();
}



playBtn.addEventListener('click', loadAudio);
stopBtn.addEventListener('click', stopAudio);
audioNextBtn.addEventListener('click', handleNextButtonClick);

3.1 DOM

๊ทธ๋Ÿผ ๋จผ์ € DOM ๋ถ€๋ถ„๋ถ€ํ„ฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ์งš๊ณ  ๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.

const audioContainer = document.querySelector('#audioContainer'); //HTML์—์„œ <audio> ํƒœ๊ทธ 
const playBtn = document.querySelector('.js-playBtn'); // play ๋ฒ„ํŠผ
const stopBtn = document.querySelector('.js-stopBtn') // stop ๋ฒ„ํŠผ
const audioNextBtn = document.querySelector('.js-audioNextBtn'); // next ๋ฒ„ํŠผ

3.2 Button Event

๊ฐ๊ฐ์˜ ๋ฒ„ํŠผ์„ click ํ•˜๋ฉด ์ž‘๋™ํ•˜๊ฒŒ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ๊ฐ์˜ ๋ฒ„ํŠผ๋“ค์— click ์ด๋ฒคํŠธ๋ฅผ ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

playBtn.addEventListener('click', loadAudio);
audioNextBtn.addEventListener('click', handleNextButtonClick);
stopBtn.addEventListener('click', stopAudio);

3.3 function loadAudio()

๋‹ค์Œ์œผ๋กœ ๋จผ์ € playBtn click ์‹œ ํ˜ธ์ถœํ•˜๋Š” loadAudio ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

function loadAudio() {
  const source = document.querySelector('#audioSource');
  source.src = `audio/${currentAudio}.mp3`;
  audioContainer.load();
  playAudio();
}

HTML์—์„œ <audio> ํƒœ๊ทธ ์•ˆ์— ์žˆ๋Š” <source> ํƒœ๊ทธ๋ฅผ DOM ์š”์†Œ const source๋กœ ์žก์•„์ฃผ๊ณ  source์˜ src ๊ฒฝ๋กœ๋ฅผ `` ๋ฐฑ ํ‹ฑ ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด์„œ audioํŒŒ์ผ ์•ˆ์— ์žˆ๋Š” ${currentAudio}๋Š” ์œ„์—์„œ 1๋กœ ์ดˆ๊ธฐ ๊ฐ’์„ ์ฃผ์–ด์„œ audioํด๋” ์•ˆ์˜ 1.mp3 ํŒŒ์ผ์ด src๊ฒฝ๋กœ์— ๋‹ด๊ธฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ HTML์˜ <audio> ํƒœ๊ทธ๋ฅผ DOM ์š”์†Œ๋กœ ์žก์€ audioContainer๋ฅผ load() ํ•ด์„œ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

load ํ•˜๊ณ  ๋‚˜๋ฉด ์ด์ œ load ํ•œ  audio๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” playAudio() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

3.4 function playAudio()

function playAudio() {
  audioContainer.volume = 0.2;
  audioContainer.loop = true;
  audioContainer.play();  
}

.volume์€ audio์˜ ๋ณผ๋ฅจ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค ๋ณผ๋ฅจ ํฌ๊ธฐ๋Š” 0 ~ 1๊นŒ์ง€ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

.loop ์€ ์ด audio๋ฅผ ๋ฐ˜๋ณต ์žฌ์ƒํ•  ๊ฑด์ง€ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. true = ๋ฐ˜๋ณต ์žฌ์ƒ, false = ๋ฐ˜๋ณต ์žฌ์ƒํ•˜์ง€ ์•Š์Œ

๊ทธ๋ฆฌ๊ณ  .play()๋ฅผ ํ•ด์ฃผ๊ฒŒ ๋˜๋ฉด audio, ์Œ์•…์ด ์ž˜ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

audio์˜ ๋” ๋งŽ์€ ์—ฌ๋Ÿฌ ์†์„ฑ์€ MDN์—์„œ ํ™•์ธํ•ด ๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

developer.mozilla.org/ko/docs/Web/HTML/Element/audio#%ED%8A%B9%EC%84%B1

 

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

developer.mozilla.org

3.5 function handleNextButtonClick()

์ด ํ•จ์ˆ˜๋Š” audioNextBtn click ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ๋‹ค์Œ ์Œ์•…์œผ๋กœ ๋„˜๊ธฐ๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

function handleNextButtonClick() { 

  if (currentAudio < MUSIC_COUNT) {
    currentAudio += 1;
  } else {
    currentAudio = 1;
  }
  
  audioContainer.pause();
  loadAudio();
}

๋จผ์ € ํ•จ์ˆ˜ ๋‚ด์—์„œ๋Š” ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด์„œ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด ํ˜„์žฌ audio ์ˆœ์„œ๊ฐ€ ์ „์ฒด audio ๊ฐœ์ˆ˜๋ณด๋‹ค ์ ์œผ๋ฉด ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์ž‘์„ฑํ•œ currentAudio +1์„ ํ•ด์ฃผ๊ณ  ์•„๋‹ˆ๋ฉด  1๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด์„œ ์ฒ˜์Œ audio๋กœ ๋˜๋Œ์•„๊ฐ€๊ฒŒ๋” ์ž‘์„ฑํ•˜๊ณ ,

ํ˜„์žฌ ์žฌ์ƒ๋˜๊ณ  ์žˆ๋Š” audio๋ฅผ ์ •์ง€์‹œํ‚ค๊ณ  loadAudio ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰๋˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ฒ˜์Œ next ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— currentAudio๋Š” 2๊ฐ€ ๋  ๊ฒƒ์ด๊ณ ,

loadAudio ํ•จ์ˆ˜์—์„œ๋Š” ์œ„์—์„œ ์„ค๋ช…ํ–ˆ๋“ฏ์ด curruntAudio ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•ด์„œ  2.mp3๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

3.6 function stopAudio()

stopBtn์— click ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ audio๋ฅผ ์ค‘๋‹จ์‹œํ‚ค๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

function stopAudio() {
  audioContainer.pause();  
}

ํ•œ ์ค„ ๋ฐ–์— ๋˜์ง€ ์•Š์ง€๋งŒ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ํ•˜๋Š”์ง€ ๋ณด๊ธฐ ์ข‹๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•จ์ˆ˜๋กœ ์ž‘์„ฑํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

.pause() ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด play ํ•˜๊ณ  ์žˆ๋˜ audio๋ฅผ ์ค‘์ง€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“• ๋งˆ๋ฌด๋ฆฌ

์ „์ฒด์ ์ธ ์ฝ”๋“œ๋ฅผ Repl ์‚ฌ์ดํŠธ์— ์˜ฌ๋ ค๋†“๊ฒ ์Šต๋‹ˆ๋‹ค. 

์ฝ”๋“œ ์˜ˆ์ œ ๋งํฌ

 

audio play example-1

audio play example

repl.it

์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์‹œ๋ ค๋ฉด ๊ฐ€์šด๋ฐ ์ƒ๋‹จ ์ชฝ์— Run ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ HTML์—์„œ <audio> ํƒœ๊ทธ์™€ <source> ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์ž‘์„ฑํ•˜๋‹ˆ๊น ๋ฒ„ํŠผ ํด๋ฆญ์œผ๋กœ audio ์Œ์•…์„ ์žฌ์ƒํ•˜๊ณ , ์ •์ง€ํ•˜๊ณ , ๋„˜๊ธฐ๋Š” ๊ธฐ๋Šฅ ๊ตฌํ˜„์ด ๋กœ์ปฌ๊ณผ ๊นƒ ํ—ˆ๋ธŒ ํผ๋ธ”๋ฆฌ์‹ฑ ์ดํ›„์—๋„ audio๊ฐ€ ์ž˜ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค!

์ง์ ‘ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์‹œ๊ณ  ์‹คํ–‰๋„ ํ•ด๋ณด๋ฉด์„œ ์‘์šฉ๋„ ํ•˜์‹œ๊ณ  ์กฐ๊ธˆ์ด๋‚˜๋งˆ ๋„์›€์ด ๋˜์—ˆ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•
profile

๐Ÿ’ป์šฉ๋‡ฝ ๊ฐœ๋ฐœ ๋…ธํŠธ๐Ÿ’ป

@์šฉ๋‡ฝ

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!