apple developer

home tech-notes github twitter stackoverflow dev.to

Adding a Video to a Scene

Play video in your scene by adding a video node.


A video node renders a video at a given size and location in your scene with no exposed player controls. You might use a video node to animate visual behaviors that would be expensive to define using a collection of textures.

Be aware that a video node offers only a subset of the features available to the SKSpriteNode class. The following are a video node’s relevant limitations:

When a video node is created, its size property is initialized to the base size of the video content, but you can change it. The video content is automatically stretched to the new size. As with a sprite node, the anchorPoint property defines where the content is displayed relative to the node position.

The following code initializes the video node using a video file stored in the app bundle and then adds the node to the scene. It calls the node’s play() method to start the video playback.

let sample = SKVideoNode(fileNamed: "sample.mov")
sample.position = CGPoint(x: frame.midX,
                          y: frame.midY)

You control playback using the node’s play() and pause() methods.

If you need more precise control over the video playback behavior, you can use AVFoundation to create an AVPlayer object for your video content and then use this object to initialize the SKVideoNode node. Then, instead of using the node’s playback methods, you use the AVPlayer object to control playback. The video content is automatically displayed in the video node.

download this page as .md

download this page as .pdf

back to SpriteKit documentation