Add MPRIS volume control

Fixes #776.
This commit is contained in:
Jonathan Müller
2022-07-11 19:55:16 +02:00
parent 2499f574ef
commit d9c51063f4
2 changed files with 28 additions and 1 deletions

View File

@ -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 = {

View File

@ -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);
}