-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathmpris.js
More file actions
94 lines (79 loc) · 2.8 KB
/
mpris.js
File metadata and controls
94 lines (79 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import dbus from 'dbus-next';
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) => {
// 更新 Mpris 状态前将位置设为0, 否则 OSDLyrics 获取到的进度是上首音乐切换时的进度
player.getPosition = () => 0;
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(','),
'xesam:url': metadata.url,
};
});
ipcMain.on('playerCurrentTrackTime', (e, position) => {
player.getPosition = () => position * 1000 * 1000;
try { player.seeked(position * 1000 * 1000); } catch {}
});
ipcMain.on('seeked', (e, position) => {
try { player.seeked(position * 1000 * 1000); } catch {}
});
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;
});
}
export async function createDbus(window) {
const bus = dbus.sessionBus();
const Variant = dbus.Variant;
const osdService = await bus.getProxyObject(
'org.osdlyrics.Daemon',
'/org/osdlyrics/Lyrics'
);
const osdInterface = osdService.getInterface('org.osdlyrics.Lyrics');
ipcMain.on('sendLyrics', async (e, { track, lyrics }) => {
const metadata = {
title: new Variant('s', track.name),
artist: new Variant('s', track.ar.map(ar => ar.name).join(', ')),
};
await osdInterface.SetLyricContent(metadata, Buffer.from(lyrics));
window.webContents.send('saveLyricFinished');
});
}