iOS Developer Library

Developer

SpriteKit Framework Reference SKVideoNode Class Reference

Options
Deployment Target:

On This Page
Language:

SKVideoNode

Inheritance


Conforms To


Import Statement


Swift

import SpriteKit

Objective-C

@import SpriteKit;

Availability


Available in iOS 7.0 and later.

An SKVideoNode object is a node that uses the AV Foundation framework to display live video content.

Like any other node, you can put the movie node anywhere inside the node tree and Sprite Kit will render it properly. For example, you might use a video node to animate some visual behaviors that would be expensive to define using a collection of textures.

A video node offers only a subset of the features available to the SKSpriteNode class:

  • A video node is always stretched uniformly.

  • A video node cannot be colorized.

  • A video node always uses an alpha blend mode.

  • A video node cannot use custom shaders or lighting.

When a video node is created, its size property is initialized to the base size of the video content, but you can change it if you want. 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.

Displaying a video in a scene 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.

Listing 1Displaying a video in a scene
  • SKVideoNode *sample = [SKVideoNode videoNodeWithVideoFileNamed:@"sample.m4v"];
  • sample.position = CGPointMake(CGRectGetMidX(self.frame),
  • CGRectGetMidY(self.frame));
  • [self addChild: sample];
  • [sample play];

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 AV Foundation 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. For more information, see AVFoundation Programming Guide.

  • Creates and initializes a new video node using a video file stored in the app bundle.

    Declaration

    Swift

    init!(videoFileNamed videoFile: String!) -> SKVideoNode

    Objective-C

    + (SKVideoNode *)videoNodeWithVideoFileNamed:(NSString *)videoFile

    Parameters

    videoFile

    The name of the video file.

    Return Value

    A new video node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Creates and initializes a video node using an existing AVPlayer object.

    Declaration

    Objective-C

    + (SKVideoNode *)videoNodeWithAVPlayer:(AVPlayer *)player

    Parameters

    player

    A player object.

    Return Value

    A new video node.

    Discussion

    You can use the AVPlayer object to control playback.

    Import Statement

    Objective-C

    @import SpriteKit;

    Availability

    Available in iOS 7.0 and later.

  • Creates and initializes a video node using a URL that points to a video file.

    Declaration

    Swift

    init!(videoURL videoURL: NSURL!) -> SKVideoNode

    Objective-C

    + (SKVideoNode *)videoNodeWithVideoURL:(NSURL *)videoURL

    Parameters

    videoURL

    The URL for the video to play.

    Return Value

    An initialized video node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Initializes a video node using a video file stored in the app bundle.

    Declaration

    Swift

    init!(videoFileNamed videoFile: String)

    Objective-C

    - (instancetype)initWithVideoFileNamed:(NSString *)videoFile

    Parameters

    videoFile

    The name of the video file.

    Return Value

    An initialized video node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • init(AVPlayer:) - initWithAVPlayer: Designated Initializer

    Initializes a video node using an existing AVPlayer object.

    Declaration

    Swift

    init!(AVPlayer player: AVPlayer!)

    Objective-C

    - (instancetype)initWithAVPlayer:(AVPlayer *)player

    Parameters

    player

    A player object.

    Return Value

    An initialized video node.

    Discussion

    You can use the AVPlayer object to control playback.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • init(videoURL:) - initWithVideoURL: Designated Initializer

    Initializes a video node using a URL that points to a video file.

    Declaration

    Swift

    init!(videoURL url: NSURL)

    Objective-C

    - (instancetype)initWithVideoURL:(NSURL *)url

    Parameters

    url

    The URL for the video to play.

    Return Value

    An initialized video node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Defines the point in the sprite that corresponds to the node’s position.

    Declaration

    Swift

    var anchorPoint: CGPoint

    Objective-C

    @property(nonatomic) CGPoint anchorPoint

    Discussion

    You specify the anchor point using the unit coordinate space. The default value is (0.5,0.5), which means that the video is centered on the node’s position.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • size size Property

    The dimensions of the video node, in points.

    Declaration

    Swift

    var size: CGSize

    Objective-C

    @property(nonatomic) CGSize size

    Discussion

    The default value is the size of the video used to instantiate the node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Starts video playback.

    Declaration

    Swift

    func play()

    Objective-C

    - (void)play

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Pauses video playback.

    Declaration

    Swift

    func pause()

    Objective-C

    - (void)pause

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.