From d9c51063f4367e083f63802ec291eea3cad88697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Mon, 11 Jul 2022 19:55:16 +0200 Subject: [PATCH] Add MPRIS volume control Fixes #776. --- plugins/shortcuts/mpris.js | 18 +++++++++++++++++- providers/song-info-front.js | 11 +++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/plugins/shortcuts/mpris.js b/plugins/shortcuts/mpris.js index 49fc8d96..3bb256d0 100644 --- a/plugins/shortcuts/mpris.js +++ b/plugins/shortcuts/mpris.js @@ -19,7 +19,7 @@ function setupMPRIS() { function registerMPRIS(win) { const songControls = getSongControls(win); - const { playPause, next, previous } = songControls; + const { playPause, next, previous, volumeMinus10, volumePlus10 } = songControls; try { const secToMicro = n => Math.round(Number(n) * 1e6); const microToSec = n => Math.round(Number(n) / 1e6); @@ -93,6 +93,22 @@ function registerMPRIS(win) { player.on('seek', seekBy); player.on('position', seekTo); + ipcMain.on('volumeChanged', (_, value) => { + player.volume = value; + }); + player.on('volume', (newVolume) => { + // With keyboard shortcuts we can only change the volume in increments of 10, so round it. + const deltaVolume = Math.round((newVolume - player.volume) / 10); + + if (deltaVolume > 0) { + for (let i = 0; i < deltaVolume; i++) + volumePlus10(); + } else { + for (let i = 0; i < -deltaVolume; i++) + volumeMinus10(); + } + }); + registerCallback(songInfo => { if (player) { const data = { diff --git a/providers/song-info-front.js b/providers/song-info-front.js index 3a704598..121d9cac 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -23,6 +23,7 @@ module.exports = () => { (is.linux() && config.plugins.isEnabled('shortcuts'))) { setupTimeChangeListener(); setupRepeatChangeListener(); + setupVolumeChangeListener(); } const video = $('video'); // name = "dataloaded" and abit later "dataupdated" @@ -74,3 +75,13 @@ function setupRepeatChangeListener() { // Emit the initial value as well; as it's persistent between launches. ipcRenderer.send('repeatChanged', $('#right-controls .repeat').title); } + +function setupVolumeChangeListener() { + const volumeObserver = new MutationObserver(mutations => { + ipcRenderer.send('volumeChanged', mutations[0].target.value); + }); + volumeObserver.observe($('#right-controls .volume-slider'), { attributeFilter: ["value"] }); + + // Emit the initial value as well; as it's persistent between launches. + ipcRenderer.send('volumeChanged', $('#right-controls .volume-slider').value); +}