Skip to content

Instantly share code, notes, and snippets.

@Richienb
Created February 22, 2020 18:00
Show Gist options
  • Save Richienb/b169c9b28bf09c9b5d8e7953aeeeb988 to your computer and use it in GitHub Desktop.
Save Richienb/b169c9b28bf09c9b5d8e7953aeeeb988 to your computer and use it in GitHub Desktop.
MPlayer slave mode in Node.js
"use strict"
const execa = require("execa")
const { default: ow } = require("ow")
module.exports = class Audic {
constructor(src) {
ow(src, ow.string)
this._src = src
this.playing = true
this._volume = 1.0
this._proc = execa("./lib/win32/mplayer-svn-38151-x86_64/mplayer.exe", ["-slave", src])
this._proc.stdout.on("data", (val) => {
const data = val.toString()
const matched = data.match(/A: (?<currentTime>.+) \(.+\) of (?<duration>.+) \(.+\) .+%/)
if (matched && matched.groups) {
const { currentTime, duration } = matched.groups
this.currentTime = Number(currentTime)
this.duration = Number(duration)
}
})
}
get src() {
return this._src
}
set src(value) {
ow(value, ow.string)
this._proc.stdin.write("stop\n")
this._proc.stdin.write(`loadfile "${value}"\n`)
this._src = value
}
play() {
if (!this.playing) {
this._proc.stdin.write("pause\n")
this.playing = true
}
}
pause() {
if (this.playing) {
this._proc.stdin.write("pause\n")
this.playing = false
}
}
get volume() {
return this._volume
}
set volume(value) {
ow(value, ow.number.inRange(0, 1))
this._proc.stdin.write(`volume ${value} 1\n`)
this._volume = value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment