feat: Add DiscordRichPresence | 增加DiscordRichPresence (#408)

This commit is contained in:
Vidocq 2021-03-16 17:50:22 +08:00 committed by GitHub
parent 0e6c40f32f
commit 267a678f2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 151 additions and 9 deletions

View file

@ -278,6 +278,15 @@ export default class {
});
navigator.mediaSession.setActionHandler("seekto", (event) => {
this.seek(event.seekTime);
this._updateMediaSessionPositionState();
});
navigator.mediaSession.setActionHandler("seekbackward", (event) => {
this.seek(this.seek() - (event.seekOffset || 10));
this._updateMediaSessionPositionState();
});
navigator.mediaSession.setActionHandler("seekforward", (event) => {
this.seek(this.seek() + (event.seekOffset || 10));
this._updateMediaSessionPositionState();
});
}
}
@ -299,6 +308,18 @@ export default class {
],
});
}
_updateMediaSessionPositionState() {
if ("mediaSession" in navigator === false) {
return;
}
if ("setPositionState" in navigator.mediaSession) {
navigator.mediaSession.setPositionState({
duration: ~~(this.currentTrack.dt / 1000),
playbackRate: 1.0,
position: this.seek(),
});
}
}
_nextTrackCallback() {
this._scrobble(true);
if (this.repeatMode === "one") {
@ -313,6 +334,26 @@ export default class {
return this._personalFMNextTrack;
});
}
_playDiscordPresence(track, seekTime = 0) {
if (
process.env.IS_ELECTRON !== true ||
store.state.settings.enableDiscordRichPresence === false
) {
return null;
}
let copyTrack = { ...track };
copyTrack.dt -= seekTime * 1000;
ipcRenderer.send("playDiscordPresence", copyTrack);
}
_pauseDiscordPresence(track) {
if (
process.env.IS_ELECTRON !== true ||
store.state.settings.enableDiscordRichPresence === false
) {
return null;
}
ipcRenderer.send("pauseDiscordPresence", track);
}
currentTrackID() {
const { list, current } = this._getListAndCurrent();
@ -361,12 +402,14 @@ export default class {
this._howler.pause();
this._playing = false;
document.title = "YesPlayMusic";
this._pauseDiscordPresence(this._currentTrack);
}
play() {
if (this._howler.playing()) return;
this._howler.play();
this._playing = true;
document.title = `${this._currentTrack.name} · ${this._currentTrack.ar[0].name} - YesPlayMusic`;
this._playDiscordPresence(this._currentTrack, this.seek());
}
playOrPause() {
if (this._howler.playing()) {
@ -376,7 +419,11 @@ export default class {
}
}
seek(time = null) {
if (time !== null) this._howler.seek(time);
if (time !== null) {
this._howler.seek(time);
if (this._playing)
this._playDiscordPresence(this._currentTrack, this.seek());
}
return this._howler === null ? 0 : this._howler.seek();
}
mute() {
@ -388,7 +435,9 @@ export default class {
}
}
setOutputDevice() {
if (this._howler._sounds.length <= 0) return;
if (this._howler._sounds.length <= 0 || !this._howler._sounds[0]._node) {
return;
}
this._howler._sounds[0]._node.setSinkId(store.state.settings.outputDevice);
}