Navigation
These archived docs are for Phaser 2.4.3 Phaser 3 docs can be found on newdocs.phaser.io.
Phaser CE docs can be found on the Phaser CE Documentation site.

Constructor

Phaser. Video

new Video(game, key, url)

A Video object that takes a previously loaded Video from the Phaser Cache and handles playback of it.

Alternatively it takes a getUserMedia feed from an active webcam and streams the contents of that to the Video instead (see startMediaStream method)

The video can then be applied to a Sprite as a texture. If multiple Sprites share the same Video texture and playback changes (i.e. you pause the video, or seek to a new time) then this change will be seen across all Sprites simultaneously.

Due to a bug in IE11 you cannot play a video texture to a Sprite in WebGL. For IE11 force Canvas mode.

If you need each Sprite to be able to play a video fully independently then you will need one Video object per Sprite. Please understand the obvious performance implications of doing this, and the memory required to hold videos in RAM.

On some mobile browsers such as iOS Safari, you cannot play a video until the user has explicitly touched the screen. This works in the same way as audio unlocking. Phaser will handle the touch unlocking for you, however unlike with audio it's worth noting that every single Video needs to be touch unlocked, not just the first one. You can use the changeSource method to try and work around this limitation, but see the method help for details.

Small screen devices, especially iPod and iPhone will launch the video in its own native video player, outside of the Safari browser. There is no way to avoid this, it's a device imposed limitation.

Parameters
Name Type Argument Default Description
game Phaser.Game

A reference to the currently running game.

key string | null <optional>
null

The key of the video file in the Phaser.Cache that this Video object will play. Set to null or leave undefined if you wish to use a webcam as the source. See startMediaStream to start webcam capture.

url string | null <optional>
null

If the video hasn't been loaded then you can provide a full URL to the file here (make sure to set key to null)

Source code: gameobjects/Video.js (Line 35)

Public Properties

disableTextureUpload : boolean

If true this video will never send its image data to the GPU when its dirty flag is true. This only applies in WebGL.

Source code: gameobjects/Video.js (Line 72)

game : Phaser.Game

A reference to the currently running game.

Source code: gameobjects/Video.js (Line 43)

height : number

The height of the video in pixels.

Source code: gameobjects/Video.js (Line 61)

isStreaming : boolean

Is there a streaming video source? I.e. from a webcam.

Source code: gameobjects/Video.js (Line 137)

key : string

The key of the Video in the Cache, if stored there. Will be null if this Video is using the webcam instead.

Default Value
  • null
Source code: gameobjects/Video.js (Line 49)

loop : boolean

Gets or sets if the Video is set to loop. Please note that at present some browsers (i.e. Chrome) do not support seamless video looping. If the video isn't yet set this will always return false.

Source code: gameobjects/Video.js (Line 1330)

mute : boolean

Gets or sets the muted state of the Video.

Source code: gameobjects/Video.js (Line 1187)

onAccess : Phaser.Signal

This signal is dispatched if the user allows access to their webcam.

Source code: gameobjects/Video.js (Line 98)

onChangeSource : Phaser.Signal

This signal is dispatched if the Video source is changed. It sends 3 parameters: a reference to the Video object and the new width and height of the new video source.

Source code: gameobjects/Video.js (Line 88)

onComplete : Phaser.Signal

This signal is dispatched when the Video completes playback, i.e. enters an 'ended' state. Videos set to loop will never dispatch this signal.

Source code: gameobjects/Video.js (Line 93)

onError : Phaser.Signal

This signal is dispatched if an error occurs either getting permission to use the webcam (for a Video Stream) or when trying to play back a video file.

Source code: gameobjects/Video.js (Line 103)

onPlay : Phaser.Signal

This signal is dispatched when the Video starts to play. It sends 3 parameters: a reference to the Video object, if the video is set to loop or not and the playback rate.

Source code: gameobjects/Video.js (Line 83)

onTimeout : Phaser.Signal

This signal is dispatched if when asking for permission to use the webcam no response is given within a the Video.timeout limit. This may be because the user has picked Not now in the permissions window, or there is a delay in establishing the LocalMediaStream.

Source code: gameobjects/Video.js (Line 110)

paused : boolean

Gets or sets the paused state of the Video. If the video is still touch locked (such as on iOS devices) this call has no effect.

Source code: gameobjects/Video.js (Line 1227)

playbackRate : number

Gets or sets the playback rate of the Video. This is the speed at which the video is playing.

Source code: gameobjects/Video.js (Line 1307)

[readonly] playing : boolean

True if the video is currently playing (and not paused or ended), otherwise false.

Source code: gameobjects/Video.js (Line 1361)

retry : integer

The current retry attempt.

Source code: gameobjects/Video.js (Line 152)

retryInterval : integer

The number of ms between each retry at monitoring the status of a downloading video.

Default Value
  • 500
Source code: gameobjects/Video.js (Line 158)

retryLimit : integer

When starting playback of a video Phaser will monitor its readyState using a setTimeout call. The setTimeout happens once every Video.retryInterval ms. It will carry on monitoring the video state in this manner until the retryLimit is reached and then abort.

Default Value
  • 20
Source code: gameobjects/Video.js (Line 146)

[readonly] snapshot : Phaser.BitmapData

A snapshot grabbed from the video. This is initially black. Populate it by calling Video.grab(). When called the BitmapData is updated with a grab taken from the current video playing or active video stream. If Phaser has been compiled without BitmapData support this property will always be null.

Source code: gameobjects/Video.js (Line 273)

texture : PIXI.Texture

The PIXI.Texture.

Source code: gameobjects/Video.js (Line 248)

textureFrame : Phaser.Frame

The Frame this video uses for rendering.

Source code: gameobjects/Video.js (Line 254)

timeout : integer

The amount of ms allowed to elapsed before the Video.onTimeout signal is dispatched while waiting for webcam access.

Default Value
  • 15000
Source code: gameobjects/Video.js (Line 116)

touchLocked : boolean

true if this video is currently locked awaiting a touch event. This happens on some mobile devices, such as iOS.

Source code: gameobjects/Video.js (Line 78)

type : number

The const type of this object.

Source code: gameobjects/Video.js (Line 67)

video :HTMLVideoElement

The HTML Video Element that is added to the document.

Source code: gameobjects/Video.js (Line 127)

videoStream :MediaStream

The Video Stream data. Only set if this Video is streaming from the webcam via startMediaStream.

Source code: gameobjects/Video.js (Line 132)

volume : number

Gets or sets the volume of the Video, a value between 0 and 1. The value given is clamped to the range 0 to 1.

Source code: gameobjects/Video.js (Line 1275)

width : number

The width of the video in pixels.

Source code: gameobjects/Video.js (Line 55)

Public Methods

add(object) → {Phaser.Video}

Updates the given Display Objects so they use this Video as their texture. This will replace any texture they will currently have set.

Parameters
Name Type Description
object Phaser.Sprite | Array.<Phaser.Sprite> | Phaser.Image | Array.<Phaser.Image>

Either a single Sprite/Image or an Array of Sprites/Images.

Returns

This Video object for method chaining.

Source code: gameobjects/Video.js (Line 774)

addToWorld(x, y, anchorX, anchorY, scaleX, scaleY) → {Phaser.Image}

Creates a new Phaser.Image object, assigns this Video to be its texture, adds it to the world then returns it.

Parameters
Name Type Argument Default Description
x number <optional>
0

The x coordinate to place the Image at.

y number <optional>
0

The y coordinate to place the Image at.

anchorX number <optional>
0

Set the x anchor point of the Image. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.

anchorY number <optional>
0

Set the y anchor point of the Image. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.

scaleX number <optional>
1

The horizontal scale factor of the Image. A value of 1 means no scaling. 2 would be twice the size, and so on.

scaleY number <optional>
1

The vertical scale factor of the Image. A value of 1 means no scaling. 2 would be twice the size, and so on.

Returns

The newly added Image object.

Source code: gameobjects/Video.js (Line 803)

changeSource(src, autoplay) → {Phaser.Video}

On some mobile browsers you cannot play a video until the user has explicitly touched the video to allow it. Phaser handles this via the setTouchLock method. However if you have 3 different videos, maybe an "Intro", "Start" and "Game Over" split into three different Video objects, then you will need the user to touch-unlock every single one of them.

You can avoid this by using just one Video object and simply changing the video source. Once a Video element is unlocked it remains unlocked, even if the source changes. So you can use this to your benefit to avoid forcing the user to 'touch' the video yet again.

As you'd expect there are limitations. So far we've found that the videos need to be in the same encoding format and bitrate. This method will automatically handle a change in video dimensions, but if you try swapping to a different bitrate we've found it cannot render the new video on iOS (desktop browsers cope better).

When the video source is changed the video file is requested over the network. Listen for the onChangeSource signal to know when the new video has downloaded enough content to be able to be played. Previous settings such as the volume and loop state are adopted automatically by the new video.

Parameters
Name Type Argument Default Description
src string

The new URL to change the video.src to.

autoplay boolean <optional>
true

Should the video play automatically after the source has been updated?

Returns

This Video object for method chaining.

Source code: gameobjects/Video.js (Line 924)

complete()

Called when the video completes playback (reaches and ended state). Dispatches the Video.onComplete signal.

Source code: gameobjects/Video.js (Line 608)

connectToMediaStream(video, stream) → {Phaser.Video}

Connects to an external media stream for the webcam, rather than using a local one.

Parameters
Name Type Description
video HTMLVideoElement

The HTML Video Element that the stream uses.

stream MediaStream

The Video Stream data.

Returns

This Video object for method chaining.

Source code: gameobjects/Video.js (Line 296)

createVideoFromBlob(blob) → {Phaser.Video}

Creates a new Video element from the given Blob. The Blob must contain the video data in the correct encoded format. This method is typically called by the Phaser.Loader and Phaser.Cache for you, but is exposed publicly for convenience.

Parameters
Name Type Description
blob Blob

The Blob containing the video data: Blob([new Uint8Array(data)])

Returns

This Video object for method chaining.

Source code: gameobjects/Video.js (Line 492)

createVideoFromURL(url, autoplay) → {Phaser.Video}

Creates a new Video element from the given URL.

Parameters
Name Type Argument Default Description
url string

The URL of the video.

autoplay boolean <optional>
false

Automatically start the video?

Returns

This Video object for method chaining.

Source code: gameobjects/Video.js (Line 515)

destroy()

Destroys the Video object. This calls Video.stop and then Video.removeVideoElement. If any Sprites are using this Video as their texture it is up to you to manage those.

Source code: gameobjects/Video.js (Line 1111)

grab(clear, alpha, blendMode) → {Phaser.BitmapData}

Grabs the current frame from the Video or Video Stream and renders it to the Video.snapshot BitmapData.

You can optionally set if the BitmapData should be cleared or not, the alpha and the blend mode of the draw.

If you need more advanced control over the grabbing them call Video.snapshot.copy directly with the same parameters as BitmapData.copy.

Parameters
Name Type Argument Default Description
clear boolean <optional>
false

Should the BitmapData be cleared before the Video is grabbed? Unless you are using alpha or a blend mode you can usually leave this set to false.

alpha number <optional>
1

The alpha that will be set on the video before drawing. A value between 0 (fully transparent) and 1, opaque.

blendMode string <optional>
null

The composite blend mode that will be used when drawing. The default is no blend mode at all. This is a Canvas globalCompositeOperation value such as 'lighter' or 'xor'.

Returns

A reference to the Video.snapshot BitmapData object for further method chaining.

Source code: gameobjects/Video.js (Line 1045)

play(loop, playbackRate) → {Phaser.Video}

Starts this video playing if it's not already doing so.

Parameters
Name Type Argument Default Description
loop boolean <optional>
false

Should the video loop automatically when it reaches the end? Please note that at present some browsers (i.e. Chrome) do not support seamless video looping.

playbackRate number <optional>
1

The playback rate of the video. 1 is normal speed, 2 is x2 speed, and so on. You cannot set a negative playback rate.

Returns

This Video object for method chaining.

Source code: gameobjects/Video.js (Line 620)

removeVideoElement()

Removes the Video element from the DOM by calling parentNode.removeChild on itself. Also removes the autoplay and src attributes and nulls the reference.

Source code: gameobjects/Video.js (Line 1081)

render()

If the game is running in WebGL this will push the texture up to the GPU if it's dirty. This is called automatically if the Video is being used by a Sprite, otherwise you need to remember to call it in your render function. If you wish to suppress this functionality set Video.disableTextureUpload to true.

Source code: gameobjects/Video.js (Line 829)

setTouchLock()

Sets the Input Manager touch callback to be Video.unlock. Required for mobile video unlocking. Mostly just used internally.

Source code: gameobjects/Video.js (Line 1003)

startMediaStream(captureAudio, width, height) → {Phaser.Video}

Instead of playing a video file this method allows you to stream video data from an attached webcam.

As soon as this method is called the user will be prompted by their browser to "Allow" access to the webcam. If they allow it the webcam feed is directed to this Video. Call Video.play to start the stream.

If they block the webcam the onError signal will be dispatched containing the NavigatorUserMediaError or MediaStreamError event.

You can optionally set a width and height for the stream. If set the input will be cropped to these dimensions. If not given then as soon as the stream has enough data the video dimensions will be changed to match the webcam device. You can listen for this with the onChangeSource signal.

Parameters
Name Type Argument Default Description
captureAudio boolean <optional>
false

Controls if audio should be captured along with video in the video stream.

width integer <optional>

The width is used to create the video stream. If not provided the video width will be set to the width of the webcam input source.

height integer <optional>

The height is used to create the video stream. If not provided the video height will be set to the height of the webcam input source.

Returns

This Video object for method chaining or false if the device doesn't support getUserMedia.

Source code: gameobjects/Video.js (Line 322)

stop() → {Phaser.Video}

Stops the video playing.

This removes all locally set signals.

If you only wish to pause playback of the video, to resume at a later time, use Video.paused = true instead. If the video hasn't finished downloading calling Video.stop will not abort the download. To do that you need to call Video.destroy instead.

If you are using a video stream from a webcam then calling Stop will disconnect the MediaStream session and disable the webcam.

Returns

This Video object for method chaining.

Source code: gameobjects/Video.js (Line 704)

unlock()

Enables the video on mobile devices, usually after the first touch. If the SoundManager hasn't been unlocked then this will automatically unlock that as well. Only one video can be pending unlock at any one time.

Source code: gameobjects/Video.js (Line 1016)

updateTexture(event, width, height)

Called automatically if the video source changes and updates the internal texture dimensions. Then dispatches the onChangeSource signal.

Parameters
Name Type Argument Description
event object <optional>

The event which triggered the texture update.

width integer <optional>

The new width of the video. If undefined video.videoWidth is used.

height integer <optional>

The new height of the video. If undefined video.videoHeight is used.

Source code: gameobjects/Video.js (Line 557)