feat: mpris support (#1279)

* feat: mpris support

* fix: 修复网页版无法播放

* fix: 修复在 Player.js 里 ipcRenderer 为空时引发的错误

* docs: update README

* docs: update README

Co-authored-by: memorydream <34763046+memorydream@users.noreply.github.com>
This commit is contained in:
CNawalol 2022-01-30 00:37:12 +08:00 committed by GitHub
parent 5a5fb1f191
commit d15b58d805
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 235 additions and 8 deletions

View File

@ -37,6 +37,7 @@
- 🟥 支持 Last.fm Scrobble
- ☁️ 支持音乐云盘
- ⌨️ 自定义快捷键和全局快捷键
- 🎧 支持Mpris
- 🛠 更多特性开发中
## 📦️ 安装

View File

@ -50,8 +50,10 @@
"extract-zip": "^2.0.1",
"howler": "^2.2.3",
"js-cookie": "^2.2.1",
"jsbi": "^4.1.0",
"lodash": "^4.17.20",
"md5": "^2.3.0",
"mpris-service": "^2.1.2",
"music-metadata": "^7.5.3",
"node-vibrant": "^3.1.6",
"nprogress": "^0.2.0",
@ -69,7 +71,8 @@
"vue-i18n": "^8.22.0",
"vue-router": "^3.4.3",
"vue-slider-component": "^3.2.5",
"vuex": "^3.4.0"
"vuex": "^3.4.0",
"x11": "^2.3.0"
},
"devDependencies": {
"@types/node": "^17.0.0",

View File

@ -14,6 +14,7 @@ import {
isLinux,
isDevelopment,
isCreateTray,
isCreateMpris,
} from '@/utils/platform';
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib';
import { startNeteaseMusicApi } from './electron/services';
@ -29,6 +30,7 @@ import { EventEmitter } from 'events';
import express from 'express';
import expressProxy from 'express-http-proxy';
import Store from 'electron-store';
import { createMpris } from '@/electron/mpris';
const clc = require('cli-color');
const log = text => {
console.log(`${clc.blueBright('[background.js]')} ${text}`);
@ -113,6 +115,14 @@ class Background {
// handle app events
this.handleAppEvents();
// disable chromium mpris
if (isCreateMpris) {
app.commandLine.appendSwitch(
'disable-features',
'HardwareMediaKeyHandling,MediaSessionService'
);
}
}
async initDevtools() {
@ -362,6 +372,11 @@ class Background {
if (this.store.get('settings.enableGlobalShortcut') !== false) {
registerGlobalShortcut(this.window, this.store);
}
// create mpris
if (isCreateMpris) {
createMpris(this.window);
}
});
app.on('activate', () => {

View File

@ -87,4 +87,8 @@ export function ipcRenderer(vueInstance) {
value,
});
});
ipcRenderer.on('setPosition', (event, position) => {
player._howler.seek(position);
});
}

62
src/electron/mpris.js Normal file
View File

@ -0,0 +1,62 @@
import { ipcMain, app } from 'electron';
export function createMpris(window) {
const Player = require('mpris-service');
const renderer = window.webContents;
const player = Player({
name: 'yesplaymusic',
identity: 'YesPlayMusic',
});
player.on('next', () => renderer.send('next'));
player.on('previous', () => renderer.send('previous'));
player.on('playpause', () => renderer.send('play'));
player.on('play', () => renderer.send('play'));
player.on('pause', () => renderer.send('play'));
player.on('quit', () => app.exit());
player.on('position', args =>
renderer.send('setPosition', args.position / 1000 / 1000)
);
player.on('loopStatus', () => renderer.send('repeat'));
player.on('shuffle', () => renderer.send('shuffle'));
ipcMain.on('player', (e, { playing }) => {
player.playbackStatus = playing
? Player.PLAYBACK_STATUS_PLAYING
: Player.PLAYBACK_STATUS_PAUSED;
});
ipcMain.on('metadata', (e, metadata) => {
player.metadata = {
'mpris:trackid': player.objectPath('track/' + metadata.trackId),
'mpris:artUrl': metadata.artwork[0].src,
'mpris:length': metadata.length * 1000 * 1000,
'xesam:title': metadata.title,
'xesam:album': metadata.album,
'xesam:artist': metadata.artist.split(','),
};
});
ipcMain.on('playerCurrentTrackTime', (e, position) => {
player.getPosition = () => position * 1000 * 1000;
});
ipcMain.on('switchRepeatMode', (e, mode) => {
switch (mode) {
case 'off':
player.loopStatus = Player.LOOP_STATUS_NONE;
break;
case 'one':
player.loopStatus = Player.LOOP_STATUS_TRACK;
break;
case 'on':
player.loopStatus = Player.LOOP_STATUS_PLAYLIST;
break;
}
});
ipcMain.on('switchShuffle', (e, shuffle) => {
player.shuffle = shuffle;
});
}

View File

@ -9,7 +9,7 @@ import { personalFM, fmTrash } from '@/api/others';
import store from '@/store';
import { isAccountLoggedIn } from '@/utils/auth';
import { trackUpdateNowPlaying, trackScrobble } from '@/api/lastfm';
import { isCreateTray } from '@/utils/platform';
import { isCreateMpris, isCreateTray } from '@/utils/platform';
const electron =
process.env.IS_ELECTRON === true ? window.require('electron') : null;
@ -229,6 +229,9 @@ export default class {
if (this._howler === null) return;
this._progress = this._howler.seek();
localStorage.setItem('playerCurrentTrackTime', this._progress);
if (isCreateMpris) {
ipcRenderer.send('playerCurrentTrackTime', this._progress);
}
}, 1000);
}
_getNextTrack() {
@ -478,7 +481,7 @@ export default class {
return;
}
let artists = track.ar.map(a => a.name);
navigator.mediaSession.metadata = new window.MediaMetadata({
const metadata = {
title: track.name,
artist: artists.join(','),
album: track.al.name,
@ -489,7 +492,14 @@ export default class {
sizes: '512x512',
},
],
});
length: this.currentTrackDuration,
trackId: this.current,
};
navigator.mediaSession.metadata = new window.MediaMetadata(metadata);
if (isCreateMpris) {
ipcRenderer.send('metadata', metadata);
}
}
_updateMediaSessionPositionState() {
if ('mediaSession' in navigator === false) {
@ -799,9 +809,15 @@ export default class {
} else {
this.repeatMode = 'on';
}
if (isCreateMpris) {
ipcRenderer.send('switchRepeatMode', this.repeatMode);
}
}
switchShuffle() {
this.shuffle = !this.shuffle;
if (isCreateMpris) {
ipcRenderer.send('switchShuffle', this.shuffle);
}
}
switchReversed() {
this.reversed = !this.reversed;

View File

@ -4,3 +4,4 @@ export const isLinux = process.platform === 'linux';
export const isDevelopment = process.env.NODE_ENV === 'development';
export const isCreateTray = isWindows || isLinux || isDevelopment;
export const isCreateMpris = isLinux;

View File

@ -151,6 +151,10 @@ module.exports = {
args[0]['IS_ELECTRON'] = true;
return args;
});
config.resolve.alias.set(
'jsbi',
path.join(__dirname, 'node_modules/jsbi/dist/jsbi-cjs.js')
);
},
// 渲染线程的配置文件
chainWebpackRendererProcess: config => {

129
yarn.lock
View File

@ -1406,6 +1406,11 @@
"@nodelib/fs.scandir" "2.1.4"
fastq "^1.6.0"
"@nornagon/put@0.0.8":
version "0.0.8"
resolved "https://registry.yarnpkg.com/@nornagon/put/-/put-0.0.8.tgz#9d497ec46c9364acc3f8b59aa3cf8ee4134ae337"
integrity sha512-ugvXJjwF5ldtUpa7D95kruNJ41yFQDEKyF5CW4TgKJnh+W/zmlBzXXeKTyqIgwMFrkePN2JqOBqcF0M0oOunow==
"@npmcli/move-file@^1.0.1":
version "1.1.2"
resolved "https://registry.npmjs.org/@npmcli/move-file/-/move-file-1.1.2.tgz"
@ -2152,6 +2157,14 @@ NeteaseCloudMusicApi@^4.5.2:
tunnel "^0.0.6"
yargs "^17.1.1"
abstract-socket@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/abstract-socket/-/abstract-socket-2.1.1.tgz#243a7e6e6ff65bb9eab16a22fa90699b91e528f7"
integrity sha512-YZJizsvS1aBua5Gd01woe4zuyYBGgSMeqDOB6/ChwdTI904KP6QGtJswXl4hcqWxbz86hQBe++HWV0hF1aGUtA==
dependencies:
bindings "^1.2.1"
nan "^2.12.1"
accepts@~1.3.4, accepts@~1.3.5, accepts@~1.3.7:
version "1.3.7"
resolved "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz"
@ -2749,7 +2762,7 @@ binaryextensions@^4.15.0:
resolved "https://registry.npmjs.org/binaryextensions/-/binaryextensions-4.15.0.tgz"
integrity sha512-MkUl3szxXolQ2scI1PM14WOT951KnaTNJ0eMKg7WzOI4kvSxyNo/Cygx4LOBNhwyINhAuSQpJW1rYD9aBSxGaw==
bindings@^1.3.0, bindings@^1.5.0:
bindings@^1.2.1, bindings@^1.3.0, bindings@^1.5.0:
version "1.5.0"
resolved "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz"
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
@ -4222,6 +4235,21 @@ dayjs@^1.8.36:
resolved "https://registry.npmjs.org/dayjs/-/dayjs-1.10.4.tgz"
integrity sha512-RI/Hh4kqRc1UKLOAf/T5zdMMX5DQIlDxwUe3wSyMMnEbGunnpENCdbUgM+dW7kXidZqCttBrmw7BhN4TMddkCw==
dbus-next@^0.9.2:
version "0.9.2"
resolved "https://registry.yarnpkg.com/dbus-next/-/dbus-next-0.9.2.tgz#d69a24ea5b54d90ab6bd543c7b81bcb965a99f93"
integrity sha512-tzQq/+wrTZ2yU+U5PoeXc97KABhX2v55C/T0finH3tSKYuI8H/SqppIFymBBrUHcK13LvEGY3vdj3ikPPenL5g==
dependencies:
"@nornagon/put" "0.0.8"
event-stream "3.3.4"
hexy "^0.2.10"
jsbi "^2.0.5"
long "^4.0.0"
safe-buffer "^5.1.1"
xml2js "^0.4.17"
optionalDependencies:
abstract-socket "^2.0.0"
de-indent@^1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/de-indent/-/de-indent-1.0.2.tgz"
@ -4660,7 +4688,7 @@ duplexer3@^0.1.4:
resolved "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz"
integrity sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=
duplexer@^0.1.1:
duplexer@^0.1.1, duplexer@~0.1.1:
version "0.1.2"
resolved "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz"
integrity sha512-jtD6YG370ZCIi/9GTaJKQxWTZD045+4R4hTk/x1UyoqadyJ9x9CgSi1RlVDQF8U2sxLLSnFkCaMihqljHIWgMg==
@ -5253,6 +5281,19 @@ event-pubsub@4.3.0:
resolved "https://registry.npmjs.org/event-pubsub/-/event-pubsub-4.3.0.tgz"
integrity sha512-z7IyloorXvKbFx9Bpie2+vMJKKx1fH1EN5yiTfp8CiLOTptSYy1g8H4yDpGlEdshL1PBiFtBHepF2cNsqeEeFQ==
event-stream@3.3.4:
version "3.3.4"
resolved "https://registry.yarnpkg.com/event-stream/-/event-stream-3.3.4.tgz#4ab4c9a0f5a54db9338b4c34d86bfce8f4b35571"
integrity sha1-SrTJoPWlTbkzi0w02Gv86PSzVXE=
dependencies:
duplexer "~0.1.1"
from "~0"
map-stream "~0.1.0"
pause-stream "0.0.11"
split "0.3"
stream-combiner "~0.0.4"
through "~2.3.1"
eventemitter3@^4.0.0:
version "4.0.7"
resolved "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz"
@ -5834,6 +5875,11 @@ from2@^2.1.0:
inherits "^2.0.1"
readable-stream "^2.0.0"
from@~0:
version "0.1.7"
resolved "https://registry.yarnpkg.com/from/-/from-0.1.7.tgz#83c60afc58b9c56997007ed1a768b3ab303a44fe"
integrity sha1-g8YK/Fi5xWmXAH7Rp2izqzA6RP4=
fs-constants@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz"
@ -6350,6 +6396,11 @@ hex-color-regex@^1.1.0:
resolved "https://registry.npmjs.org/hex-color-regex/-/hex-color-regex-1.1.0.tgz"
integrity sha512-l9sfDFsuqtOqKDsQdqrMRk0U85RZc0RtOR9yPI7mRVOa4FsR/BVnZ0shmQRM96Ji99kYZP/7hn1cedc1+ApsTQ==
hexy@^0.2.10:
version "0.2.11"
resolved "https://registry.yarnpkg.com/hexy/-/hexy-0.2.11.tgz#9939c25cb6f86a91302f22b8a8a72573518e25b4"
integrity sha512-ciq6hFsSG/Bpt2DmrZJtv+56zpPdnq+NQ4ijEFrveKN0ZG1mhl/LdT1NQZ9se6ty1fACcI4d4vYqC9v8EYpH2A==
highlight.js@^10.7.1:
version "10.7.2"
resolved "https://registry.npmjs.org/highlight.js/-/highlight.js-10.7.2.tgz"
@ -7334,6 +7385,16 @@ js-yaml@^4.0.0:
dependencies:
argparse "^2.0.1"
jsbi@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/jsbi/-/jsbi-2.0.5.tgz#82589011da87dc59b4b549d94dcef51a9155f6fe"
integrity sha512-TzO/62Hxeb26QMb4IGlI/5X+QLr9Uqp1FPkwp2+KOICW+Q+vSuFj61c8pkT6wAns4WcK56X7CmSHhJeDGWOqxQ==
jsbi@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/jsbi/-/jsbi-4.1.0.tgz#f0c25ed8881bc09891334b03a3952efaa7add337"
integrity sha512-384Z4keIsJtYpnVggsxaB255MZctILbxv+ihtwoWPF7KNOlYHn1LFpRnUw5qsAspUAA2+I7qzjVJxVYtHVjxNw==
jsbn@~0.1.0:
version "0.1.1"
resolved "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz"
@ -7728,6 +7789,11 @@ loglevel@^1.6.8:
resolved "https://registry.npmjs.org/loglevel/-/loglevel-1.7.1.tgz"
integrity sha512-Hesni4s5UkWkwCGJMQGAh71PaLUmKFM60dHvq0zi/vDhhrzuk+4GgNbTXJ12YYQJn6ZKBDNIjYcuQGKudvqrIw==
long@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==
lower-case@^1.1.1:
version "1.1.4"
resolved "https://registry.npmjs.org/lower-case/-/lower-case-1.1.4.tgz"
@ -7799,6 +7865,11 @@ map-cache@^0.2.2:
resolved "https://registry.npmjs.org/map-cache/-/map-cache-0.2.2.tgz"
integrity sha1-wyq9C9ZSXZsFFkW7TyasXcmKDb8=
map-stream@~0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/map-stream/-/map-stream-0.1.0.tgz#e56aa94c4c8055a16404a0674b78f215f7c8e194"
integrity sha1-5WqpTEyAVaFkBKBnS3jyFffI4ZQ=
map-visit@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/map-visit/-/map-visit-1.0.0.tgz"
@ -8159,6 +8230,15 @@ move-concurrently@^1.0.1:
rimraf "^2.5.4"
run-queue "^1.0.3"
mpris-service@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/mpris-service/-/mpris-service-2.1.2.tgz#ed6db2574f48e1fc356413fd3fc8e857d93d2e7b"
integrity sha512-AC6WepCnFWwOME9OWplHZ8ps/BB+g9QrEpUKCv7wX82fDPzR3nPrypOFmL/Fm0JloEAu6QTWSfDLLc6mM/jinw==
dependencies:
dbus-next "^0.9.2"
deep-equal "^1.0.1"
source-map-support "^0.5.11"
mri@1.1.4:
version "1.1.4"
resolved "https://registry.npmjs.org/mri/-/mri-1.1.4.tgz"
@ -8703,6 +8783,11 @@ os-browserify@^0.3.0:
resolved "https://registry.npmjs.org/os-browserify/-/os-browserify-0.3.0.tgz"
integrity sha1-hUNzx/XCMVkU/Jv8a9gjj92h7Cc=
os-homedir@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3"
integrity sha1-/7xJiDNuDoM94MFox+8VISGqf7M=
os-tmpdir@~1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz"
@ -9047,6 +9132,13 @@ path-type@^4.0.0:
resolved "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz"
integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==
pause-stream@0.0.11:
version "0.0.11"
resolved "https://registry.yarnpkg.com/pause-stream/-/pause-stream-0.0.11.tgz#fe5a34b0cbce12b5aa6a2b403ee2e73b602f1445"
integrity sha1-/lo0sMvOErWqaitAPuLnO2AvFEU=
dependencies:
through "~2.3"
pbkdf2@^3.0.3:
version "3.1.2"
resolved "https://registry.npmjs.org/pbkdf2/-/pbkdf2-3.1.2.tgz"
@ -10832,6 +10924,14 @@ source-map-resolve@^0.5.0:
source-map-url "^0.4.0"
urix "^0.1.0"
source-map-support@^0.5.11:
version "0.5.21"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f"
integrity sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
source-map-support@^0.5.19, source-map-support@~0.5.12:
version "0.5.19"
resolved "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz"
@ -10928,6 +11028,13 @@ split2@^4.0.0:
resolved "https://registry.npmjs.org/split2/-/split2-4.1.0.tgz"
integrity sha512-VBiJxFkxiXRlUIeyMQi8s4hgvKCSjtknJv/LVYbrgALPwf5zSKmEwV9Lst25AkvMDnvxODugjdl6KZgwKM1WYQ==
split@0.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/split/-/split-0.3.3.tgz#cd0eea5e63a211dfff7eb0f091c4133e2d0dd28f"
integrity sha1-zQ7qXmOiEd//frDwkcQTPi0N0o8=
dependencies:
through "2"
sprintf-js@^1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.2.tgz"
@ -11011,6 +11118,13 @@ stream-browserify@^2.0.1:
inherits "~2.0.1"
readable-stream "^2.0.2"
stream-combiner@~0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/stream-combiner/-/stream-combiner-0.0.4.tgz#4d5e433c185261dde623ca3f44c586bcf5c4ad14"
integrity sha1-TV5DPBhSYd3mI8o/RMWGvPXErRQ=
dependencies:
duplexer "~0.1.1"
stream-each@^1.1.0:
version "1.2.3"
resolved "https://registry.npmjs.org/stream-each/-/stream-each-1.2.3.tgz"
@ -11492,7 +11606,7 @@ through2@^2.0.0, through2@~2.0.0:
readable-stream "~2.3.6"
xtend "~4.0.1"
through@^2.3.6:
through@2, through@^2.3.6, through@~2.3, through@~2.3.1:
version "2.3.8"
resolved "https://registry.npmjs.org/through/-/through-2.3.8.tgz"
integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=
@ -12680,6 +12794,13 @@ ws@^7.3.1:
resolved "https://registry.npmjs.org/ws/-/ws-7.4.4.tgz"
integrity sha512-Qm8k8ojNQIMx7S+Zp8u/uHOx7Qazv3Yv4q68MiWWWOJhiwG5W3x7iqmRtJo8xxrciZUY4vRxUTJCKuRnF28ZZw==
x11@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/x11/-/x11-2.3.0.tgz#bdaccedccdac2482fd560f0511e7bf2bd9e1dbf2"
integrity sha1-vazO3M2sJIL9Vg8FEee/K9nh2/I=
dependencies:
os-homedir "^1.0.1"
xdg-basedir@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz"
@ -12700,7 +12821,7 @@ xml-parse-from-string@^1.0.0:
resolved "https://registry.npmjs.org/xml-parse-from-string/-/xml-parse-from-string-1.0.1.tgz"
integrity sha1-qQKekp09vN7RafPG4oI42VpdWig=
xml2js@^0.4.5:
xml2js@^0.4.17, xml2js@^0.4.5:
version "0.4.23"
resolved "https://registry.npmjs.org/xml2js/-/xml2js-0.4.23.tgz"
integrity sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==