From 110bc1cbbf66fb782e323b8b680b48ea9e1a3981 Mon Sep 17 00:00:00 2001 From: Tom Amberson Date: Wed, 21 Jun 2023 16:33:53 -0600 Subject: [PATCH 1/5] Add mpris-service --- index.js | 7 +++++++ package.json | 1 + 2 files changed, 8 insertions(+) diff --git a/index.js b/index.js index 40a59fd..1b09409 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,13 @@ var server = require('http').createServer(app); var io = require('socket.io')(server); var fs = require('fs'); +var Player = require("mpris-service"); +var player = Player({ + name: "Patiobar", + identity: "Node.js pianobar controller", + supportedInterfaces: ["player"], +}); + var fifo = process.env.PIANOBAR_FIFO || 'ctl'; var listenPort = process.env.PATIOBAR_PORT || 3000; diff --git a/package.json b/package.json index 262e06c..0eca318 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "A web frontend for pianobar, which is a CLI frontend for Pandora.", "dependencies": { "express": "^4.17.1", + "mpris-service": "^2.1.2", "socket.io": "2.4.0" } } From 65164ad47ffc78bbd45041383799b64292d4ce1d Mon Sep 17 00:00:00 2001 From: Tom Amberson Date: Wed, 21 Jun 2023 16:34:50 -0600 Subject: [PATCH 2/5] Set mpris player metadata from current song info --- index.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1b09409..199b3e5 100644 --- a/index.js +++ b/index.js @@ -23,8 +23,23 @@ function readCurrentSong() { var currentSong = fs.readFileSync(process.env.HOME + '/.config/pianobar/currentSong').toString() if (currentSong) { - var a = currentSong.split(',,,'); - io.emit('start', { artist: a[0], title: a[1], album: a[2], coverArt: a[3], rating: a[4], stationName: a[5] }); + const a = currentSong.split(",,,"); + io.emit("start", { + artist: a[0], + title: a[1], + album: a[2], + coverArt: a[3], + rating: a[4], + stationName: a[5], + }); + player.metadata = { + "mpris:length": 0, + "mpris:artUrl": a[3], + "xesam:title": a[1], + "xesam:album": a[2], + "xesam:artist": [a[0]], + }; + player.playbackStatus = "Playing"; } } From 644b3b54f1a96bc8dc740f3165f23a2d2a665226 Mon Sep 17 00:00:00 2001 From: Tom Amberson Date: Wed, 21 Jun 2023 16:35:40 -0600 Subject: [PATCH 3/5] Add handler for mpris events --- index.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/index.js b/index.js index 199b3e5..a227b15 100644 --- a/index.js +++ b/index.js @@ -124,3 +124,26 @@ app.post('/lovehate', function(request, response) { }); + +// mpris events +const events = [ + "raise", + "quit", + "next", + "previous", + "pause", + "playpause", + "stop", + "play", + "seek", + "position", + "open", + "volume", + "loopStatus", + "shuffle", +]; +events.forEach(function (eventName) { + player.on(eventName, function () { + console.log("Event:", eventName, arguments); + }); +}); From 1c1f52f6f86aa19bcd7cda2245c5587a2337faa6 Mon Sep 17 00:00:00 2001 From: Tom Amberson Date: Wed, 21 Jun 2023 16:39:11 -0600 Subject: [PATCH 4/5] Send pianobar commands from mpris handler --- index.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/index.js b/index.js index a227b15..4e46aa1 100644 --- a/index.js +++ b/index.js @@ -145,5 +145,32 @@ const events = [ events.forEach(function (eventName) { player.on(eventName, function () { console.log("Event:", eventName, arguments); + switch (eventName) { + case "quit": + PidoraCTL("q"); + process.exit(); + break; + case "playpause": + PidoraCTL("p"); + if (player.playbackStatus !== "Playing") { + player.playbackStatus = "Playing"; + } else { + player.playbackStatus = "Paused"; + } + break; + case "play": + PidoraCTL("P"); + player.playbackStatus = "Playing"; + break; + case "pause": + PidoraCTL("S"); + player.playbackStatus = "Paused"; + break; + case "next": + PidoraCTL("n"); + break; + default: + break; + } }); }); From 420308f00df61127eb3a269fad5dec08b286648e Mon Sep 17 00:00:00 2001 From: Tom Amberson Date: Wed, 21 Jun 2023 16:39:41 -0600 Subject: [PATCH 5/5] Set extra mpris metadata --- index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.js b/index.js index 4e46aa1..6bcb2f6 100644 --- a/index.js +++ b/index.js @@ -174,3 +174,7 @@ events.forEach(function (eventName) { } }); }); + +player.canSeek = false; +player.canGoPrevious = false; +player.playbackStatus = "Stopped";