Shahnawaz
Shahnawaz

Reputation: 646

Cannot play video with SKVideoNode in SpriteKit Swift

Here is the following code I code to play the video. If I click a button then it calls a method which has this code snippet for playing the video:

if let urlStr = NSBundle.mainBundle().pathForResource("into_main_v3", ofType: "mp4")
    {
        let url = NSURL(fileURLWithPath: urlStr)
        print(url)
        player = AVPlayer(URL: url)

        videoNode = SKVideoNode(AVPlayer: player!)
        videoNode?.position = CGPointMake(frame.size.width/2, frame.size.height/2)
        videoNode?.size = CGSize(width: self.size.width, height: self.size.height)
        videoNode?.zPosition = 1
        addChild(videoNode!)

        videoNode!.play()
    }

I get this message printed in my console: : calling -display has no effect.

When I click the button it goes to this method but never goes beyond the urlStr variable. I triple checked and the resource is available in my project for the mp4 video but it still won't run! Its not in any folder, the path is correct. It worked before but suddenly its not working, no idea why!

EDIT:

Problem solved: The problem was with copying the mp4 file in build phase to make sure its copied in the iPad.

Upvotes: 1

Views: 1599

Answers (2)

Dominick
Dominick

Reputation: 174

Update for Swift 5 since CGPointMake and a few others are obsolete now:

if let urlStr = Bundle.main.path(forResource: "into_main_v3", ofType: "mp4")
    {
        let url = NSURL(fileURLWithPath: urlStr)
        print(url)
        let player = AVPlayer(url: url as URL)

        let videoNode = SKVideoNode(avPlayer: player)
        videoNode.position = CGPoint(x: frame.size.width/2, y: frame.size.height/2)
        videoNode.size = CGSize(width: self.size.width, height: self.size.height)
        videoNode.zPosition = 123
        addChild(videoNode)

        videoNode.play()
    }

Upvotes: 0

FatherTimeBK
FatherTimeBK

Reputation: 71

So, when I was trying to figure this out, I had a couple of things wrong... First, my URL wasn't right so I'd check to ensure it is (the file name was wrong). Second, when I was loading the video, I was using a .mov extension which did not work for me. I changed it to .mp4 and the video loaded. Third, the display of the video has a lot of do with where you place it in the scene. I can't tell much about your scene from the above code sample, but it is important to make sure you are placing the video node correctly using the right coordinates and size or it won't show. I would try changing your size to (1,1) and see if it shows up.

Upvotes: 2

Related Questions