Skip to content

Commit

Permalink
Fix video ready state assess error. (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
zenoslin committed Apr 28, 2022
1 parent 8812e31 commit 35fb689
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions web/src/core/video-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,34 @@ export interface TimeRange {
}

const playVideoElement = (videoElement: HTMLVideoElement) => {
if (IS_WECHAT && window.WeixinJSBridge) {
window.WeixinJSBridge.invoke(
'getNetworkType',
{},
() => {
videoElement.play();
},
() => {
videoElement.play();
},
);
} else {
videoElement.play();
}
return new Promise((resolve) => {
const play = () => {
videoElement
.play()
.then(() => {
resolve(true);
})
.catch((event: DOMException) => {
Log.error(event.message);
resolve(false);
});
};

if (IS_WECHAT && window.WeixinJSBridge) {
window.WeixinJSBridge.invoke(
'getNetworkType',
{},
() => {
play();
},
() => {
play();
},
);
} else {
play();
}
});
};

export class VideoReader {
Expand Down Expand Up @@ -60,24 +74,17 @@ export class VideoReader {
resolve(true);
} else {
// Wait for initialization to complete
const canplayCallback = () => {
if (this.videoEl === null) {
Log.errorByCode(ErrorCode.VideoElementNull);
} else {
window.requestAnimationFrame(() => {
if (this.videoEl === null) {
Log.errorByCode(ErrorCode.VideoElementNull);
} else {
this.videoEl.pause();
this.hadPlay = true;
resolve(true);
}
});
removeListener(this.videoEl, 'playing', canplayCallback);
}
};
addListener(this.videoEl, 'playing', canplayCallback);
playVideoElement(this.videoEl);
playVideoElement(this.videoEl).then(() => {
window.requestAnimationFrame(() => {
if (this.videoEl === null) {
Log.errorByCode(ErrorCode.VideoElementNull);
} else {
this.videoEl.pause();
this.hadPlay = true;
resolve(true);
}
});
});
}
} else {
if (Math.round(targetTime * this.frameRate) === Math.round(currentTime * this.frameRate)) {
Expand Down Expand Up @@ -128,11 +135,11 @@ export class VideoReader {

private seek(resolve: (value: unknown) => void, targetTime: number, play = true) {
let isCallback = false;
const timeupdateCallback = () => {
const canplayCallback = () => {
if (this.videoEl === null) {
Log.errorByCode(ErrorCode.VideoElementNull);
} else {
removeListener(this.videoEl, 'timeupdate', timeupdateCallback);
removeListener(this.videoEl, 'canplay', canplayCallback);
if (play && this.videoEl.paused) {
playVideoElement(this.videoEl);
} else if (!play && !this.videoEl.paused) {
Expand All @@ -147,7 +154,7 @@ export class VideoReader {
Log.errorByCode(ErrorCode.VideoElementNull);
return;
}
addListener(this.videoEl, 'timeupdate', timeupdateCallback);
addListener(this.videoEl, 'canplay', canplayCallback);
this.videoEl!.currentTime = targetTime;
// Timeout
setTimeout(() => {
Expand All @@ -156,7 +163,7 @@ export class VideoReader {
resolve(false);
Log.errorByCode(ErrorCode.VideoElementNull);
} else {
removeListener(this.videoEl, 'timeupdate', timeupdateCallback);
removeListener(this.videoEl, 'canplay', canplayCallback);
resolve(true);
}
}
Expand Down

0 comments on commit 35fb689

Please sign in to comment.