Skip to content

Commit

Permalink
Fix: sync player may undefine
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Oct 8, 2023
1 parent 5801c0c commit a6aac5a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/plugins/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ const debounceTime = 500;

export const sync = (cbk: callback): resould => {
const debounce = debounces(debounceTime);
let player: Artplayer;
let player: Artplayer | undefined = undefined;
const publishSeek = useDebounceFn((currentTime: number) => {
if (player.option.isLive) return;
if (!player || player.option.isLive) return;
cbk["set-player-status"](
JSON.stringify({
Type: WsMessageType.SEEK,
Expand All @@ -42,12 +42,12 @@ export const sync = (cbk: callback): resould => {
}, debounceTime);

const setAndNoPublishSeek = (seek: number) => {
if (player.option.isLive || Math.abs(player.currentTime - seek) < 2) return;
if (!player || player.option.isLive || Math.abs(player.currentTime - seek) < 2) return;
player.currentTime = seek;
};

const publishPlay = () => {
if (player.option.isLive) return;
if (!player || player.option.isLive) return;
cbk["set-player-status"](
JSON.stringify({
Type: WsMessageType.PLAY,
Expand All @@ -60,14 +60,14 @@ export const sync = (cbk: callback): resould => {
const publishPlayDebounce = debounce(publishPlay);

const setAndNoPublishPlay = () => {
if (player.option.isLive || player.playing) return;
if (!player || player.option.isLive || player.playing) return;
player.off("play", publishPlayDebounce);
player.once("play", () => {
player.on("play", publishPlayDebounce);
!player || player.on("play", publishPlayDebounce);
});
player.play().catch(() => {
player.muted = true;
player.play();
!player || (player.muted = true);
!player || player.play();
ElNotification({
title: "温馨提示",
type: "info",
Expand All @@ -77,7 +77,7 @@ export const sync = (cbk: callback): resould => {
};

const publishPause = () => {
if (player.option.isLive) return;
if (!player || player.option.isLive) return;
cbk["set-player-status"](
JSON.stringify({
Type: WsMessageType.PAUSE,
Expand All @@ -90,16 +90,16 @@ export const sync = (cbk: callback): resould => {
const publishPauseDebounce = debounce(publishPause);

const setAndNoPublishPause = () => {
if (player.option.isLive || !player.playing) return;
if (!player || player.option.isLive || !player.playing) return;
player.off("pause", publishPauseDebounce);
player.once("pause", () => {
player.on("pause", publishPauseDebounce);
!player || player.on("pause", publishPauseDebounce);
});
player.pause();
};

const publishRate = () => {
if (player.option.isLive) return;
if (!player || player.option.isLive) return;
cbk["set-player-status"](
JSON.stringify({
Type: WsMessageType.RATE,
Expand All @@ -111,10 +111,10 @@ export const sync = (cbk: callback): resould => {
};

const setAndNoPublishRate = (rate: number) => {
if (player.option.isLive || player.playbackRate === rate) return;
if (!player || player.option.isLive || player.playbackRate === rate) return;
player.off("video:ratechange", publishRate);
player.once("video:ratechange", () => {
player.on("video:ratechange", publishRate);
!player || player.on("video:ratechange", publishRate);
});
player.playbackRate = rate;
};
Expand Down Expand Up @@ -145,6 +145,7 @@ export const sync = (cbk: callback): resould => {
art.on("video:ratechange", publishRate);

art.on("destroy", () => {
player = undefined;
art.off("play", publishPlayDebounce);
art.off("pause", publishPauseDebounce);
art.off("seek", publishSeek);
Expand Down
2 changes: 2 additions & 0 deletions src/views/Cinema.vue
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ watchers.push(
case WsMessageType.CURRENT_MOVIE: {
room.currentMovie = jsonData.current.movie;
room.currentMovieStatus = jsonData.current.status;
syncPlugin.setAndNoPublishSeek(jsonData.current.status.seek);
syncPlugin.setAndNoPublishRate(jsonData.current.status.rate);
break;
}
Expand Down

0 comments on commit a6aac5a

Please sign in to comment.