From 519440b5491d56a7c1d0f2490ecf358dd68b97f8 Mon Sep 17 00:00:00 2001 From: vapormusic Date: Fri, 24 Dec 2021 15:04:42 +0700 Subject: [PATCH] save last played track, time and queue --- src/renderer/index.js | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/renderer/index.js b/src/renderer/index.js index 67071787..27bd8bf9 100644 --- a/src/renderer/index.js +++ b/src/renderer/index.js @@ -332,6 +332,52 @@ const app = new Vue({ this.library.albums.displayListing = this.library.albums.listing } + window.onbeforeunload = function(e) { + window.localStorage.setItem("currentTrack",JSON.stringify(app.mk.nowPlayingItem)) + window.localStorage.setItem("currentTime",JSON.stringify(app.mk.currentPlaybackTime)) + window.localStorage.setItem("currentQueue",JSON.stringify(app.mk.queue.items)) + }; + + // load last played track + try { + lastItem = window.localStorage.getItem("currentTrack") + time = window.localStorage.getItem("currentTime") + queue = window.localStorage.getItem("currentQueue") + if (lastItem != null) { + lastItem = JSON.parse(lastItem) + var kind = lastItem.attributes.playParams.kind; + var truekind = (!kind.endsWith("s")) ? (kind + "s") : kind; + app.mk.setQueue({ [truekind]: [lastItem.attributes.playParams.id] }) + app.mk.mute() + setTimeout(() =>{ + app.mk.play().then(() => { + app.mk.pause().then(() => { + if (time != null) { + app.mk.seekToTime(time) + } + app.mk.unmute() + if (queue != null ){ + queue = JSON.parse(queue) + if (queue && queue.length > 0){ + let ids = queue.map ( e => (e.playParams ? e.playParams.id : (e.attributes.playParams ? e.attributes.playParams.id : '') )) + if (ids.length > 0) { + for (id of ids){ + try{ + app.mk.playLater({songs: [id] }) + } catch (err){} + } + } + } + } + + }) + + })},1000) + + } + + } catch (e) {console.log(e)} + MusicKit.getInstance().videoContainerElement = document.getElementById("apple-music-video-player")