Client/Javascript

[javascript / ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] audio play (์˜ค๋””์˜ค ์žฌ์ƒ) ๋ฐฉ๋ฒ•, Audio ๊ฐ์ฒด ์˜ˆ์‹œ

meeeeejin 2020. 6. 19. 18:37

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์˜ค๋””์˜ค ์žฌ์ƒํ•˜๊ธฐ

 

HTML5๋ถ€ํ„ฐ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ Audio ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด ์˜ค๋””์˜ค๋ฅผ ์žฌ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์˜ค๋””์˜ค ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์™€ ์žฌ์ƒํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

 

1
2
var audio = new Audio('audio_file.mp3');
audio.play();
cs

 

audio๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด Audio ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , play() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์Œ์•…์„ ์žฌ์ƒํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. 

new Audio('audio_file.mp3')์— ()์•ˆ์—๋Š” ์ž์‹ ์ด ๋„ฃ๊ณ  ์‹ถ์€ ์Œ์•… ํŒŒ์ผ์˜ src๋ฅผ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

๋งŒ์•ฝ HTML ํŒŒ์ผ์— Audio ๊ฐ์ฒด๊ฐ€ ์žˆ๊ณ , ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์ด๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์žฌ์ƒํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ ๋ฐฉ๋ฒ•์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. 

 

1
document.getElementById('myAudio').play();
cs

 

'myAudio' ์ž๋ฆฌ์— ์žฌ์ƒํ•  Audio ๊ฐ์ฒด์˜ id๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

 

 

 

Audio ๊ฐ์ฒด

 

์Œ์•…์„ ์žฌ์ƒํ•˜๊ธฐ ์œ„ํ•œ Audio ๊ฐ์ฒด์—๋Š” play()๋ง๊ณ ๋„ ๋‹ค์–‘ํ•œ ์†์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ๋“ค์ด ์žˆ๋‹ค. 

 

var audio = document.getElementById("myAudio");

์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋กœ audio๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ id๊ฐ€ myAudio์ธ Audio ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค๊ณ  ๊ฐ€์ •

 

<์ž์ฃผ ์“ฐ์ด๋Š” Audio ๊ฐ์ฒด์˜ ์†์„ฑ>

  • audio.autoplay = true;
    // audio๊ฐ€ load ๋  ๋•Œ ์ž๋™์žฌ์ƒ ๋จ
  • audio.currentTime = 5;
    // audio์˜ ์žฌ์ƒ์‹œ์ ์„ 5์ดˆ๋กœ ์„ค์ •ํ•จ
  • audio.duration;
    // audio์˜ ๊ธธ์ด๋ฅผ ์ดˆ(seconds) ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜
  • audio.loop = true;
    // audio๋ฅผ ๋ฐ˜๋ณต ์žฌ์ƒํ•จ
  • audio.src = "my_audio.mp3";
    // audio์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•จ(URL)
  • audio.volume = 0.2;
    // audio์˜ ์Œ๋Ÿ‰์„ 0.2๋กœ ์ง€์ •ํ•จ
    // ์Œ๋Ÿ‰์€ 0.0 ~ 1.0 ์‚ฌ์ด ๊ฐ’์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ , 1.0์ด ๊ฐ€์žฅ ํฐ ์Œ๋Ÿ‰

 

 

<์ž์ฃผ ์“ฐ์ด๋Š” Audio ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ>

  • audio.play();
    // audio๋ฅผ ์žฌ์ƒํ•จ
  • audio.pause();
    // audio๋ฅผ ์ผ์‹œ์ •์ง€ํ•จ
  • audio.load();
    // audio๋ฅผ ๋‹ค์‹œ loadํ•จ
    // ์ฃผ๋กœ audio์˜ src๋‚˜ ์„ค์ •์ด ๋ฐ”๋€Œ์—ˆ์„ ๊ฒฝ์šฐ์— ์‚ฌ์šฉ

 

 

Audio ๊ฐ์ฒด ์‚ฌ์šฉ ์˜ˆ์‹œ

 

<HTML ์ฝ”๋“œ>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Audio ๊ฐ์ฒด</title>
  </head>
  <body>
    <button class="btn1">play sound1</button>
    <button class="btn2">play sound2</button>
    <script src="index.js"></script>
  </body>
</html>
cs

 

 

<JavaScript ์ฝ”๋“œ>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// btn1์„ ๋ˆŒ๋ €์„ ๋•Œ sound1.mp3 ์žฌ์ƒ
document.querySelector(".btn1").addEventListener("click"function () {
  var audio1 = new Audio("sound1.mp3");
  audio1.loop = false// ๋ฐ˜๋ณต์žฌ์ƒํ•˜์ง€ ์•Š์Œ
  audio1.volume = 0.5// ์Œ๋Ÿ‰ ์„ค์ •
  audio1.play(); // sound1.mp3 ์žฌ์ƒ
});
 
// btn2๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ sound2.mp3 ์žฌ์ƒ
document.querySelector(".btn2").addEventListener("click"function () {
  var audio2 = new Audio("sound2.mp3");
  audio2.loop = true// ๋ฐ˜๋ณต์žฌ์ƒํ•˜์ง€ ์•Š์Œ
  audio2.volume = 0.5// ์Œ๋Ÿ‰ ์„ค์ •
  audio2.play(); // sound2.mp3 ์žฌ์ƒ
  setTimeout(function () { // 1์ดˆ ํ›„ sound2.mp3 ์ผ์‹œ์ •์ง€
    audio2.pause();
  }, 1000);
});
cs

 

 

 

 

์ฐธ๊ณ  ์‚ฌ์ดํŠธ: https://www.w3schools.com/tags/ref_av_dom.asp

https://stackoverflow.com/questions/9419263/playing-audio-with-javascript

 

 

๊ณต๋ถ€ํ•œ ๊ฒƒ์„ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ˆ˜์ •ํ•  ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค :)

 

 

 

728x90