Swift SpriteKit SKVideoNode only plays once

1.5k Views Asked by At

Hi I'm trying to play video on an SKVideoNode in swift this is my code that works but it only plays once if i click on VideoSprite it doesn't play but dose print "we Clicked the video" I've searched but can't seem to find an answer thanks for looking

import SpriteKit
import AVFoundation

class GameScene: SKScene
{
    var VideoSprite = SKVideoNode()

    override func didMoveToView(view: SKView)
    {
        LoadVideo("9.mp4")
    }

    func LoadVideo(FileToPlay:String)
    {
        VideoSprite =  SKVideoNode (videoFileNamed:FileToPlay)
        VideoSprite.position = CGPointMake(size.width/2, size.height/2);
        VideoSprite.name = "VideoSprite"
        VideoSprite.zPosition = 2
        addChild(VideoSprite)
    }


    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
    {
        for touch in (touches as! Set<UITouch>)
        {
            var touch: UITouch = touches.first as! UITouch
            var location = touch.locationInNode(self)
            var node = self.nodeAtPoint(location)
            if (node.name == "VideoSprite")
            {
                println("we Clicked the video")
                VideoSprite.play()
            }

        }
    }

}
1

There are 1 best solutions below

0
On

Hi I think I've cracked it i add another videoSKnode on top and rename it so the user can't click on the original video then after a delay i remove it this delay needs to be the time the video runs i use a cool function for delay i found on the internet can't remember where from so thanks to who ever wrote it i hope this might help somebody else i tried the paused bool in the update function but no luck?

import SpriteKit
import AVFoundation
import AVKit
class GameScene: SKScene
{

 var VideoSprite = SKVideoNode()// emptey Videonode

    func delay(delay:Double, closure:()->())// function for delay
    {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }


    override func didMoveToView(view: SKView)
    {

    LoadVideo("9.mp4") // run function to load video
    VideoSprite.pause()// pauses video ready to play
    }

    func LoadVideo(FileToPlay:String)
    {

        VideoSprite =  SKVideoNode (videoFileNamed:FileToPlay)// fill spritenode with video file
        VideoSprite.position = CGPointMake(size.width/2, size.height/2);  // set size
        VideoSprite.name = "VideoSprite" // give it a name
        VideoSprite.zPosition = 1 // set its z position
        VideoSprite.play()// play video
        addChild(VideoSprite)// add video node to the sceene
    }


    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
    {
        for touch in (touches as! Set<UITouch>)
        {
            var touch: UITouch = touches.first as! UITouch
            var location = touch.locationInNode(self)
            var node = self.nodeAtPoint(location)
    println(node.name)

            if (node.name == "VideoSprite")// we click on video
            {

                LoadVideo("9.mp4") /// load another video ontop
                VideoSprite.name = "temp" //name it temp so user cant click to add more videonodes
                delay(1.8 )// wait for time
                    {
                     self.VideoSprite.removeFromParent()// then remove
                     }

            }
        }
    }



}