Class: Video

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.

Note: On iOS if you need to detect when the user presses the 'Done' button (before the video ends) then you need to add your own event listener

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:
src/gameobjects/Video.js line 38

Members

currentTime : number

The current time of the video in seconds. If set the video will attempt to seek to that point in time.

Type:
  • number
Source:
src/gameobjects/Video.js line 1184

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.

Type:
  • boolean
Source:
src/gameobjects/Video.js line 75

<readonly> duration : number

The duration of the video in seconds.

Type:
  • number
Source:
src/gameobjects/Video.js line 1204

game : Phaser.Game

A reference to the currently running game.

Type:
Source:
src/gameobjects/Video.js line 46

height : number

The height of the video in pixels.

Type:
  • number
Source:
src/gameobjects/Video.js line 64

isStreaming : boolean

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

Type:
  • boolean
Source:
src/gameobjects/Video.js line 140

key : string

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

Type:
  • string
Default Value:
  • null
Source:
src/gameobjects/Video.js line 52

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.

Type:
  • boolean
Source:
src/gameobjects/Video.js line 1377

mute : boolean

Gets or sets the muted state of the Video.

Type:
  • boolean
Source:
src/gameobjects/Video.js line 1234

onAccess : Phaser.Signal

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

Type:
Source:
src/gameobjects/Video.js line 101

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.

Type:
Source:
src/gameobjects/Video.js line 91

onComplete : Phaser.Signal

This signal is dispatched when the Video completes playback, i.e. enters an 'ended' state. On iOS specifically it also fires if the user hits the 'Done' button at any point during playback. Videos set to loop will never dispatch this signal.

Type:
Source:
src/gameobjects/Video.js line 96

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.

Type:
Source:
src/gameobjects/Video.js line 106

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.

Type:
Source:
src/gameobjects/Video.js line 86

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.

Type:
Source:
src/gameobjects/Video.js line 113

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.

Type:
  • boolean
Source:
src/gameobjects/Video.js line 1274

playbackRate : number

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

Type:
  • number
Source:
src/gameobjects/Video.js line 1354

<readonly> playing : boolean

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

Type:
  • boolean
Source:
src/gameobjects/Video.js line 1408

<readonly> progress : number

The progress of this video. This is a value between 0 and 1, where 0 is the start and 1 is the end of the video.

Type:
  • number
Source:
src/gameobjects/Video.js line 1219

retry : integer

The current retry attempt.

Type:
  • integer
Source:
src/gameobjects/Video.js line 155

retryInterval : integer

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

Type:
  • integer
Default Value:
  • 500
Source:
src/gameobjects/Video.js line 161

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.

Type:
  • integer
Default Value:
  • 20
Source:
src/gameobjects/Video.js line 149

<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.

Type:
Source:
src/gameobjects/Video.js line 295

texture : PIXI.Texture

The PIXI.Texture.

Type:
Source:
src/gameobjects/Video.js line 270

textureFrame : Phaser.Frame

The Frame this video uses for rendering.

Type:
Source:
src/gameobjects/Video.js line 276

timeout : integer

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

Type:
  • integer
Default Value:
  • 15000
Source:
src/gameobjects/Video.js line 119

touchLocked : boolean

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

Type:
  • boolean
Source:
src/gameobjects/Video.js line 81

type : number

The const type of this object.

Type:
  • number
Source:
src/gameobjects/Video.js line 70

video : HTMLVideoElement

The HTML Video Element that is added to the document.

Type:
  • HTMLVideoElement
Source:
src/gameobjects/Video.js line 130

videoStream : MediaStream

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

Type:
  • MediaStream
Source:
src/gameobjects/Video.js line 135

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.

Type:
  • number
Source:
src/gameobjects/Video.js line 1322

width : number

The width of the video in pixels.

Type:
  • number
Source:
src/gameobjects/Video.js line 58

Methods

add(object)

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.

Source:
src/gameobjects/Video.js line 817
Returns:

This Video object for method chaining.

Type
Phaser.Video

addToWorld( [x] [, y] [, anchorX] [, anchorY] [, scaleX] [, scaleY])

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.

Source:
src/gameobjects/Video.js line 846
Returns:

The newly added Image object.

Type
Phaser.Image

changeSource(src [, autoplay])

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?

Source:
src/gameobjects/Video.js line 967
Returns:

This Video object for method chaining.

Type
Phaser.Video

complete()

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

Source:
src/gameobjects/Video.js line 630

connectToMediaStream(video, stream)

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.

Source:
src/gameobjects/Video.js line 318
Returns:

This Video object for method chaining.

Type
Phaser.Video

createVideoFromBlob(blob)

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.

Source:
src/gameobjects/Video.js line 514
Returns:

This Video object for method chaining.

Type
Phaser.Video

createVideoFromURL(url [, autoplay])

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?

Source:
src/gameobjects/Video.js line 537
Returns:

This Video object for method chaining.

Type
Phaser.Video

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:
src/gameobjects/Video.js line 1158

grab( [clear] [, alpha] [, blendMode])

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'.

Source:
src/gameobjects/Video.js line 1092
Returns:

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

Type
Phaser.BitmapData

play( [loop] [, playbackRate])

Starts this video playing.

If the video is already playing, or has been queued to play with changeSource then this method just returns.

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.

Source:
src/gameobjects/Video.js line 642
Returns:

This Video object for method chaining.

Type
Phaser.Video

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:
src/gameobjects/Video.js line 1128

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:
src/gameobjects/Video.js line 872

setTouchLock()

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

Source:
src/gameobjects/Video.js line 1050

startMediaStream( [captureAudio] [, width] [, height])

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.

Source:
src/gameobjects/Video.js line 344
Returns:

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

Type
Phaser.Video

stop()

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.

Source:
src/gameobjects/Video.js line 737
Returns:

This Video object for method chaining.

Type
Phaser.Video

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:
src/gameobjects/Video.js line 1063

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:
src/gameobjects/Video.js line 579

phaser-ce@2.9.2 is on GitHub and NPM

Documentation generated by JSDoc 3.5.4 on 2017-11-09 using Tomorrow.