Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

Commit

Permalink
fix: 外部暂停时播放状态不同步
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jan 23, 2022
1 parent e0abba1 commit 97e4f09
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
*.log

/tmp
*.zip
*.zip
.vscode
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "netease-music-crx",
"description": "能够收听VIP或变灰歌曲的网易云音乐播放器",
"license": "GPL-3.0-only",
"version": "5.4.0",
"version": "5.5.0",
"scripts": {
"build": "node scripts/build.js",
"start": "node scripts/webserver.js",
Expand Down
4 changes: 2 additions & 2 deletions src/background/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ function initContextMenu() {
}
});

subscribeKey(store, "playing", (playing) => {
subscribeKey(store, "audioPlaying", (audioPlaying) => {
const id = "togglePlaying";
chrome.contextMenus.update(id, contextMenus[id](playing));
chrome.contextMenus.update(id, contextMenus[id](audioPlaying));
});

subscribeKey(store, "userId", (userId) => {
Expand Down
22 changes: 17 additions & 5 deletions src/background/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
shuffleArr,
race,
randChinaIp,
sleep,
} from "../utils";

// 播放器
Expand All @@ -38,12 +39,13 @@ let refreshAt = 0;
// 上次暂停时间
let pausedAt = null;

const store = proxy({ ...COMMON_PROPS, dir: 1, chinaIp: null });
const store = proxy({ ...COMMON_PROPS, playing: false, dir: 1, chinaIp: null });

export async function bootstrap() {
setInterval(async () => {
await refreshLogin();
if (Date.now() - refreshAt > 13 * 60 * 60 * 1000) {
store.playing = store.audioPlaying;
await refreshStore();
}
}, 33 * 60 * 1000);
Expand All @@ -66,11 +68,13 @@ export function updateAudioTime(currentTime) {
}

export async function togglePlaying() {
let { playing } = store;
let { audioPlaying, playing } = store;
if (!audio) {
return { playing };
store.audioPlaying = false;
store.playing = false;
return { audioPlaying: false };
}
if (playing) {
if (audioPlaying) {
pausedAt = Date.now();
audio.pause();
playing = false;
Expand All @@ -90,7 +94,7 @@ export async function togglePlaying() {
}
store.playing = playing;
persistSave();
return { playing };
return { audioPlaying: !audioPlaying };
}

export function toggleMute() {
Expand Down Expand Up @@ -315,6 +319,7 @@ function getPopupData() {
const {
userId,
playing,
audioPlaying,
volume,
playMode,
playlists,
Expand All @@ -324,6 +329,7 @@ function getPopupData() {
return {
userId,
playing,
audioPlaying,
volume,
playMode,
playlists,
Expand Down Expand Up @@ -688,6 +694,12 @@ function setupAudio() {
});
}
};
audio.onplay = () => {
store.audioPlaying = true;
};
audio.onpause = () => {
store.audioPlaying = false;
};
audio.oncanplay = () => {
updateAudioState({
duration: audio.duration,
Expand Down
4 changes: 2 additions & 2 deletions src/popup/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Player() {
const [showModal, setShowModal] = useState(false);
const {
userId,
playing,
audioPlaying,
selectedSong,
selectedPlaylist,
volume,
Expand Down Expand Up @@ -100,7 +100,7 @@ export default function Player() {
onClick={() => storeUtils.togglePlaying()}
title="播放/暂停"
>
{playing ? <PauseIcon /> : <PlayArrowIcon />}
{audioPlaying ? <PauseIcon /> : <PlayArrowIcon />}
</IconButton>
<IconButton onClick={() => storeUtils.playNext()} title="下一首">
<SkipNextIcon />
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const PLAY_MODE = {
export const COMMON_PROPS = {
userId: null,
vip: false,
playing: false,
audioPlaying: false,
volume: 1,
playMode: PLAY_MODE.LOOP,
playlists: [],
Expand Down

0 comments on commit 97e4f09

Please sign in to comment.