Skip to content

Commit d9c5106

Browse files
committed
Add MPRIS volume control
Fixes #776.
1 parent 2499f57 commit d9c5106

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

plugins/shortcuts/mpris.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function setupMPRIS() {
1919

2020
function registerMPRIS(win) {
2121
const songControls = getSongControls(win);
22-
const { playPause, next, previous } = songControls;
22+
const { playPause, next, previous, volumeMinus10, volumePlus10 } = songControls;
2323
try {
2424
const secToMicro = n => Math.round(Number(n) * 1e6);
2525
const microToSec = n => Math.round(Number(n) / 1e6);
@@ -93,6 +93,22 @@ function registerMPRIS(win) {
9393
player.on('seek', seekBy);
9494
player.on('position', seekTo);
9595

96+
ipcMain.on('volumeChanged', (_, value) => {
97+
player.volume = value;
98+
});
99+
player.on('volume', (newVolume) => {
100+
// With keyboard shortcuts we can only change the volume in increments of 10, so round it.
101+
const deltaVolume = Math.round((newVolume - player.volume) / 10);
102+
103+
if (deltaVolume > 0) {
104+
for (let i = 0; i < deltaVolume; i++)
105+
volumePlus10();
106+
} else {
107+
for (let i = 0; i < -deltaVolume; i++)
108+
volumeMinus10();
109+
}
110+
});
111+
96112
registerCallback(songInfo => {
97113
if (player) {
98114
const data = {

providers/song-info-front.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = () => {
2323
(is.linux() && config.plugins.isEnabled('shortcuts'))) {
2424
setupTimeChangeListener();
2525
setupRepeatChangeListener();
26+
setupVolumeChangeListener();
2627
}
2728
const video = $('video');
2829
// name = "dataloaded" and abit later "dataupdated"
@@ -74,3 +75,13 @@ function setupRepeatChangeListener() {
7475
// Emit the initial value as well; as it's persistent between launches.
7576
ipcRenderer.send('repeatChanged', $('#right-controls .repeat').title);
7677
}
78+
79+
function setupVolumeChangeListener() {
80+
const volumeObserver = new MutationObserver(mutations => {
81+
ipcRenderer.send('volumeChanged', mutations[0].target.value);
82+
});
83+
volumeObserver.observe($('#right-controls .volume-slider'), { attributeFilter: ["value"] });
84+
85+
// Emit the initial value as well; as it's persistent between launches.
86+
ipcRenderer.send('volumeChanged', $('#right-controls .volume-slider').value);
87+
}

0 commit comments

Comments
 (0)