playerxtwo v1.1.6
PlayerXTwo (Node mpg123 player)
Install mpg123 command line audio player (Ubuntu/Debian)
sudo apt install mpg123Install PlayerXTwo to Node
npm i playerxtwoUsage
const PlayerXTwo = require('playerxtwo');
let myPlayer = new PlayerXTwo();
let volume = 30;
PlayerXTwo.getDevices((devices) => {
if(devices.get('HDA Intel'))
myPlayer.create({
device: devices.get('HDA Intel'),
volume
});
else
myPlayer.create({ volume });
});
myPlayer.on('play', function (valid) {
console.log("Audio (Play)");
});
myPlayer.on('stop', function () {
console.log("Audio (Stop)");
});
myPlayer.on('error', function (err) {
console.error("Audio (ERROR)", err)
});
myPlayer.on('pause', function () {
console.log("Audio (Pause)");
});
myPlayer.on('resume', function (data) {
const name = data && data.name? data.name: "";
console.log("Audio (Resume): "+ name);
});
myPlayer.on('end', function (data) {
const name = data && data.name? data.name: "";
console.log("Audio (End) " + name);
});
myPlayer.on('info', function (data) {
const trackName = data.StreamTitle;
console.log("Track info: " + trackName);
});
myPlayer.on('volume', function (data) {
console.log("Volume set: " + data);
myPlayer.volume = Math.floor(data);
});
player.play(__dirname+'/'+"someMusic.mp3");Device Objects
Device objects allow you to select different output sources for playback, provided you are using ALSA.
This functionality requires the aplay command, but is entirely optional.
mpg.getDevices(callback) - Gets array of ALSA output devicesdevices.get(name) - Finds device in array with given name, otherwise returns nulldevice.name - Friendly name of devicedevice.address - ALSA address of device
Player Objects
new new PlayerXTwo(device=null, volume=false) - Create new instance, optionally specifing output deviceplayer.play(file) - Plays audio from a sourceplayer.pause() - Pauses the current trackplayer.stop() - Stops the current trackplayer.setVolume(percent) - Sets the volume from 0 to 100
player.pitch(amt) - Adjusts the pitch & speed of the track up or down. The limits seem to be around -0.75 to 0.1.player.seek(progress) - Seeks through the track with progress from 0 to 1. This fails before the format event has fired.player.getProgress(callback) - Retrieve current track progress from 0 to 1player.close() - Kills the mpg123 processplayer._cmd(...) - Sends a custom command to the mpg123 CLI. Get possible commands by running mpg123 -R then typing HELP
player.next() - Next track
Song Info Variables
Theses variables hold info about the current song, and are safe to read only once the format event has fired.
player.track - Current track name (with extention). Set to null when track completes.player.file - Full file path, exactly as it was entered into player.play()player.mpeg - MPEG encoding versionplayer.sampleRate - Track sample rateplayer.channels - Number of channelsplayer.bitrate - Track bitrateplayer.length - Track length in seconds, rounded to the nearest tenthplayer.samples - Track length in raw samples