Skip to content

Commit

Permalink
feat(player): sync playing progress to mpris service on linux
Browse files Browse the repository at this point in the history
Co-authored-by: alex3236 <45303195+alex3236@users.noreply.github.com>
  • Loading branch information
Revincx and alex3236 committed Aug 26, 2023
1 parent 6ad756b commit 486b04b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/electron/mpris.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export function createMpris(window) {

ipcMain.on('playerCurrentTrackTime', (e, position) => {
player.getPosition = () => position * 1000 * 1000;
player.seeked(position * 1000 * 1000);
});

ipcMain.on('seeked', (e, position) => {
player.seeked(position * 1000 * 1000);
});

ipcMain.on('switchRepeatMode', (e, mode) => {
Expand Down
10 changes: 8 additions & 2 deletions src/utils/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ export default class {
set progress(value) {
if (this._howler) {
this._howler.seek(value);
if (isCreateMpris) {
ipcRenderer?.send('seeked', this._howler.seek());
}
}
}
get isCurrentTrackLiked() {
Expand Down Expand Up @@ -836,11 +839,14 @@ export default class {
this.play();
}
}
seek(time = null) {
seek(time = null, sendMpris = true) {
if (isCreateMpris && sendMpris && time) {
ipcRenderer?.send('seeked', time);
}
if (time !== null) {
this._howler?.seek(time);
if (this._playing)
this._playDiscordPresence(this._currentTrack, this.seek());
this._playDiscordPresence(this._currentTrack, this.seek(null, false));
}
return this._howler === null ? 0 : this._howler.seek();
}
Expand Down
2 changes: 1 addition & 1 deletion src/views/lyrics.vue
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ export default {
},
setLyricsInterval() {
this.lyricsInterval = setInterval(() => {
const progress = this.player.seek() ?? 0;
const progress = this.player.seek(null, false) ?? 0;
let oldHighlightLyricIndex = this.highlightLyricIndex;
this.highlightLyricIndex = this.lyric.findIndex((l, index) => {
const nextLyric = this.lyric[index + 1];
Expand Down

0 comments on commit 486b04b

Please sign in to comment.