0

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()
            }

        }
    }

}
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
user2164327
  • 283
  • 1
  • 2
  • 13
  • Have you had a look at http://stackoverflow.com/questions/27027648/how-do-i-control-playback-of-a-video-in-an-ios-sprite-kit-skvideonode – ABakerSmith May 31 '15 at 09:34
  • hi thanks for the reply I've looked at that thread i tried `if VideoSprite.paused == true { println("we are Paused") //VideoSprite.removeFromParent() } ` but it don't print – user2164327 May 31 '15 at 09:47
  • Do you want to restart the video when the video node is pressed? Or did you want to pause the video when it's pressed? – ABakerSmith May 31 '15 at 12:06
  • hi thanks for your reply I'm trying to restart thanks – user2164327 May 31 '15 at 16:58

1 Answers1

2

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
                     }

            }
        }
    }



}
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
user2164327
  • 283
  • 1
  • 2
  • 13