반응형

이전 포스팅: https://gusdnr69.tistory.com/category/IOT%EA%B0%9C%EB%B0%9C/node.js

 

'IOT개발/node.js' 카테고리의 글 목록

코딩, 개발, 애니덕질

gusdnr69.tistory.com

 

play-sound는  mp3 파일에 한해서 재생이 됩니다.

다양한 음원 파일을 재생할 수 있는 라이브러리를 소개하겠습니다.

 

https://dyclassroom.com/reference-javascript/work-with-audio-in-javascript-using-howler-js

 

Work with audio in JavaScript using Howler.js - Reference JavaScript - DYclassroom | Have fun learning :-)

In this tutorial we will learn about Howler.js which makes working with audio in JavaScript easy and reliable across all platforms. Click here to visit Howler.js GitHub repository. Install If you have Node and NPM installed then use the following command i

dyclassroom.com

 

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 없이 사용하셔도 무방합니다.

 

 

반응형

+ Recent posts