Node.jsで音声を再生する

node-wav-playerを利用し、MP3のファイルを再生する

node-wav-player

GitHub - futomi/node-wav-player: The node-wav-player is a Node.js module which allows you to play a wav file on the host computer. It supports Windows 10, MacOS X, and some Linux distros.
The node-wav-player is a Node.js module which allows you to play a wav file on the host computer. It supports Windows 10...

いくつか記事見てると、Windowsオンリーみたいなこと書いてあるものもあったが、MacOSにも対応している

The node-wav-player is a Node.js module which allows you to play a wav file on the host computer. It supports Windows 10, MacOS X, and some Linux distros.

https://github.com/futomi/node-wav-player

環境

MacOS v10.14.6

$ node -v
v8.15.0
$ npm -v
6.4.1

今回は、ローカルで実行して音がなることを確かめるところまで。

Sample

function playSound(path) {
  return new Promise((resolve, reject) => {
    player.play({path: path}).then(() => {
      resolve();
    }).catch((error) => {
      console.error(error);
      reject(error);
    });
  });
}

playSound('./sounds/sample.mp3');

player.stop()で停止できるよう

おまけ1:テスト用のフリー音源

こちらから拝借した。

フリーBGM(音楽素材)無料ダウンロード|DOVA-SYNDROME
フリーBGM音楽素材を無料ダウンロード配布しています。商用や非商用など、用途を問わず連絡不要でご自由にご利用頂けます。

おまけ2:WAVとMP3の違い

WAVファイルを非可逆圧縮したファイルがMP3ファイル

https://vook.vc/n/1076
タイトルとURLをコピーしました