반응형
이전 포스팅: https://gusdnr69.tistory.com/category/IOT%EA%B0%9C%EB%B0%9C/node.js
play-sound는 mp3 파일에 한해서 재생이 됩니다.
다양한 음원 파일을 재생할 수 있는 라이브러리를 소개하겠습니다.
https://dyclassroom.com/reference-javascript/work-with-audio-in-javascript-using-howler-js
Howler.js 입니다. 웹 UI를 통해서 mp3외에 다른 형식의 음원파일도 재생이 가능합니다.
저는 wav파일을 재생하기 위해서
node-aplay
를 사용하였습니다.
설치 절차
sudo apt-get update
sudo apt-get upgrade
sudo rpi-update
sudo apt-get install alsa-base alsa-utils
sudo npm install node-aplay
예시 코드
var Sound = require('node-aplay');
// fire and forget:
new Sound('/path/to/the/file/filename.wav').play();
// with ability to pause/resume:
var music = new Sound('/path/to/the/file/filename.wav');
music.play();
setTimeout(function () {
music.pause(); // pause the music after five seconds
}, 5000);
setTimeout(function () {
music.resume(); // and resume it two seconds after pausing
}, 7000);
// you can also listen for various callbacks:
music.on('complete' function () {
console.log('Done with playback!');
});
pause나 resume은 미리 변수에 sound 객체를 저장해놓아야만 가능합니다.
setTimeout 없이 사용하셔도 무방합니다.
반응형
'IOT개발 > node.js' 카테고리의 다른 글
라즈베리파이 node.js 스피커 제어하기 (0) | 2020.04.25 |
---|---|
라즈베리파이 3색 LED 제어하기 (feat node.js) (0) | 2020.03.30 |
node.js 란? 🤷♂️ (0) | 2020.03.04 |