Play sounds by shelling out with MPlayer audio player. Make it easy to play audio in Node.js.
npm install play-sound-mplayer
Since we use mplayer over the command-line to facilitate audio playback, you will also need to install a mplayer instance.
To get started, require the module in your program and create a new AudioPlayer instance.
const mplayer = require('play-sound-mplayer');
const player = new mplayer.AudioPlayer();
To play an audio source, use the play
function and pass in the file path or url of the audio source that is to be played:
player.on('start', () => {
// Do something here when the audio starts playing
});
player.on('end', () => {
// Do something here when the audio finishes playing
});
player.on('error', (error: Error) => {
// Do something here when an error thrown
});
player.play('./test/sound.mp3');
Stop the playback (without the ability to resume later) by using the stop
function:
player.on('stop', () => {
// Do something here when the audio is stopped
});
player.stop();
Pause the playback (with the ability to resume later) by using the pause
function:
player.on('pause', () => {
// Do something here when the audio is paused
});
player.pause();
Resume the playback from a paused state by using the resume
function:
player.on('resume', () => {
// Do something here when the audio is resumed
});
player.resume();
Mute the playback (with the ability to unmute later) by using the mute
function:
player.on('muted', () => {
// Do something here when the audio is muted.
});
player.mute();
Unmute the playback from a muted state by using the unMute
function:
player.on('unmuted', () => {
// Do something here when the audio is unmuted
});
player.unMute();
Set the volume of the playback by using the setVolume(0..100)
function:
player.on('volumeupdate', (volume: number) => {
// Do something here when the audio volume updated with volume: number 0..100
});
player.setVolume(30);