Video

Video is a DisplayObject for playing video content. It supports video formats such as WebM and MP4.

Inheritance

classDiagram
    DisplayObject <|-- Video

    class Video {
        +src: string
        +videoWidth: number
        +videoHeight: number
        +duration: number
        +currentTime: number
        +volume: number
        +loop: boolean
        +autoPlay: boolean
        +smoothing: boolean
        +paused: boolean
        +muted: boolean
        +loaded: boolean
        +ended: boolean
        +isVideo: boolean
        +play() Promise~void~
        +pause() void
        +seek(offset) void
    }

Properties

PropertyTypeDefaultDescription
srcstring""Specifies the URL of the video content
videoWidthnumber0An integer specifying the width of the video, in pixels
videoHeightnumber0An integer specifying the height of the video, in pixels
durationnumber0Total number of keyframes (video duration)
currentTimenumber0Current keyframe (playback position)
volumenumber1The volume, ranging from 0 (silent) to 1 (full volume)
loopbooleanfalseSpecifies whether to loop the video
autoPlaybooleantrueSetting up automatic video playback
smoothingbooleantrueSpecifies whether the video should be smoothed (interpolated) when it is scaled
pausedbooleantrueReturns whether the video is paused
mutedbooleanfalseReturns whether the video is muted
loadedbooleanfalseReturns whether the video has been loaded
endedbooleanfalseReturns whether the video has ended
isVideobooleantrueReturns whether the display object has Video functionality (read-only)

Methods

MethodReturnDescription
play()Promise<void>Plays the video file
pause()voidPauses the video playback
seek(offset: number)voidSeeks the keyframe closest to the specified location

Usage Examples

Basic Video Playback

const { Video } = next2d.media;

// Create Video object (specify width, height)
const video = new Video(640, 360);

// Set video source (loading starts automatically)
video.src = "video.mp4";

// Property settings
video.autoPlay = true;   // Auto play
video.loop = false;      // No loop
video.smoothing = true;  // Enable smoothing

// Add to stage
stage.addChild(video);

Playback Control

const { Video } = next2d.media;
const { PointerEvent } = next2d.events;

const video = new Video(640, 360);
video.autoPlay = false;  // Disable auto play
video.src = "video.mp4";

stage.addChild(video);

// Play button
playButton.addEventListener(PointerEvent.POINTER_DOWN, async () => {
    await video.play();
});

// Pause button
pauseButton.addEventListener(PointerEvent.POINTER_DOWN, () => {
    video.pause();
});

// Stop button (pause and return to start)
stopButton.addEventListener(PointerEvent.POINTER_DOWN, () => {
    video.pause();
    video.seek(0);
});

// Forward 10 seconds
forwardButton.addEventListener(PointerEvent.POINTER_DOWN, () => {
    video.seek(video.currentTime + 10);
});

// Back 10 seconds
backButton.addEventListener(PointerEvent.POINTER_DOWN, () => {
    video.seek(Math.max(0, video.currentTime - 10));
});

Event Listening

const { Video } = next2d.media;
const { VideoEvent } = next2d.events;

const video = new Video(640, 360);

// Play event
video.addEventListener(VideoEvent.PLAY, () => {
    console.log("Play requested");
});

// Playing event
video.addEventListener(VideoEvent.PLAYING, () => {
    console.log("Playback started");
});

// Pause event
video.addEventListener(VideoEvent.PAUSE, () => {
    console.log("Paused");
});

// Seek event
video.addEventListener(VideoEvent.SEEK, () => {
    console.log("Seek:", video.currentTime);
});

video.src = "video.mp4";
stage.addChild(video);

Displaying Playback Progress

const { Video } = next2d.media;

const video = new Video(640, 360);
video.src = "video.mp4";
stage.addChild(video);

// Update progress each frame
stage.addEventListener("enterFrame", () => {
    if (video.duration > 0) {
        const progress = video.currentTime / video.duration;
        progressBar.scaleX = progress;
        timeLabel.text = formatTime(video.currentTime) + " / " + formatTime(video.duration);
    }
});

function formatTime(seconds) {
    const min = Math.floor(seconds / 60);
    const sec = Math.floor(seconds % 60);
    return min + ":" + sec.toString().padStart(2, '0');
}

Volume Control

const { Video } = next2d.media;

const video = new Video(640, 360);
video.src = "video.mp4";
video.volume = 0.5;  // 50%

stage.addChild(video);

// Mute toggle
muteButton.addEventListener(PointerEvent.POINTER_DOWN, () => {
    video.muted = !video.muted;
});

Loop Playback

const { Video } = next2d.media;

const video = new Video(640, 360);
video.loop = true;  // Enable loop
video.src = "video.mp4";

stage.addChild(video);

VideoEvent

EventDescription
VideoEvent.PLAYWhen play is requested
VideoEvent.PLAYINGWhen playback starts
VideoEvent.PAUSEWhen paused
VideoEvent.SEEKWhen seeking

Supported Formats

FormatExtensionSupport
MP4 (H.264).mp4Recommended
WebM (VP8/VP9).webmSupported
Ogg Theora.ogvBrowser dependent