Skip to content

Commit

Permalink
#154 Videoの読み込み待機処理を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
ienaga committed Feb 13, 2025
1 parent bb95400 commit 5207b65
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
59 changes: 59 additions & 0 deletions packages/core/src/Next2D/service/VideoSyncService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { DisplayObject, DisplayObjectContainer } from "@next2d/display";

/**
* @description DisplayObject の子要素に Video が含まれている場合、ロードが完了するまで待機します。
* If the child element of DisplayObject contains Video, wait until the loading is complete.
*
* @param {DisplayObject} display_object
* @return {Promise<void>}
* @method
* @protected
*/
export const execute = async <D extends DisplayObject> (display_object: D): Promise<void> =>
{
switch (true) {

case display_object.isVideo:
break;

case display_object.isContainerEnabled:
{
const children = (display_object as unknown as DisplayObjectContainer).children;
for (let idx = 0; idx < children.length; ++idx) {

const displayObject = children[idx];
if (!displayObject) {
continue;
}

if (displayObject.isVideo) {
await displayObject.play();
displayObject.pause();

await new Promise<void>((resolve) =>
{
const wait = async (): Promise<void> =>
{
if (displayObject.loaded) {
displayObject.seek(0);
resolve();
} else {
requestAnimationFrame(wait);
}
};
requestAnimationFrame(wait);
});
}

if (displayObject.isContainerEnabled) {
await execute(displayObject as DisplayObjectContainer);
}
}
}
break;

default:
break;

}
};
5 changes: 5 additions & 0 deletions packages/core/src/Next2D/usecase/CaptureToCanvasUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { $player } from "../../Player";
import { $devicePixelRatio } from "../../CoreUtil";
import { execute as playerResizePostMessageService } from "../../Player/service/PlayerResizePostMessageService";
import { execute as playerTransferCanvasPostMessageService } from "../../Player/service/PlayerTransferCanvasPostMessageService";
import { execute as videoSyncService } from "../service/VideoSyncService";
import {
Matrix,
ColorTransform
Expand Down Expand Up @@ -37,6 +38,10 @@ export const execute = async <D extends DisplayObject> (
opstions: ICaptureOptions | null = null
): Promise<HTMLCanvasElement> => {

if (opstions && opstions.videoSync) {
await videoSyncService(display_object);
}

const tColorTransform = opstions && opstions.colorTransform
? opstions.colorTransform.rawData
: $COLOR_ARRAY_IDENTITY;
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/interface/ICaptureOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type {
} from "@next2d/geom";

export interface ICaptureOptions {
matrix?: Matrix | null;
colorTransform?: ColorTransform | null;
canvas?: HTMLCanvasElement | null;
matrix?: Matrix;
colorTransform?: ColorTransform;
canvas?: HTMLCanvasElement;
videoSync?: boolean;
}
2 changes: 1 addition & 1 deletion packages/media/src/Video/usecase/VideoPlayEventUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { execute as videoApplyChangesService } from "../service/VideoApplyChange
*/
export const execute = (video: Video): number =>
{
if (video.paused) {
if (video.paused || !video.loaded) {
return 0;
}

Expand Down

0 comments on commit 5207b65

Please sign in to comment.