How to play a video from url using SKVideoNode?

585 views Asked by At

I'm getting the url using PHPickerViewControllerDelegate. I've confirmed that my url has properly feed into the code but when i play the video node, no video is displayed and i dont hear any sound, so it's not a layering issue. We can call this option 1.

UPDATE: I thought i could fix the problem by using AVPlayer, so i tried two additional ways, (Option 2) putting the video file in the project, which worked but doesn't fix my issue of pulling files from my local directory. (Option 3) using a video from local directory, which didn't work. The SKVideoNode box shows but no video plays.

This tells me, it has something to do with the local directory url. Is there some privacy thing i need to include, so the video shows? I'm so close but seem so far.

Optional(file:///private/var/mobile/Containers/Data/Application/3D99929D-CFC4-418A-B215-3AE463F3AD38/tmp/.com.apple.Foundation.NSItemProvider.WMPrvs/RPReplay_Final1617465667.mp4)

import Foundation
import SpriteKit
import GameplayKit
import PhotosUI

class AnimationInventory18: SKScene {
 
   var chosenCharacterSceneUse1: CharacterSkin?
    var chosenBackgroundSceneUse1: BackgroundSkin?
    var selectedTextSceneUse1: String?
    var chosenImageSceneUse1: UIImage?
    var chosenLivePhotoSceneUse1: PHLivePhoto?
    var chosenVideoSceneUse1: URL?

    //MARK: First Party Class Initializers
    init(size: CGSize, chosenChateracterSceneUse: CharacterSkin?,selectedTextSceneUse: String?, chosenBackgroundSceneUse: BackgroundSkin?, chosenImageSceneUse: UIImage?, chosenLivePhotoSceneUse: PHLivePhoto?, chosenVideoSceneUse: URL? ){
        super.init(size:size)
        self.chosenCharacterSceneUse1 = chosenChateracterSceneUse
        self.selectedTextSceneUse1 = selectedTextSceneUse
        self.chosenBackgroundSceneUse1 = chosenBackgroundSceneUse
        self.chosenImageSceneUse1 = chosenImageSceneUse
        self.chosenLivePhotoSceneUse1 = chosenLivePhotoSceneUse
        self.chosenVideoSceneUse1 = chosenVideoSceneUse
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func niceToMeetYou() {
        videoPlay()     
    }

    func videoPlay(){
        // below print statement outputs - *Optional(file:///private/var/mobile/Containers/Data/Application/3D99929D-CFC4-418A-B215-3AE463F3AD38/tmp/.com.apple.Foundation.NSItemProvider.WMPrvs/RPReplay_Final1617465667.mp4)*
        print(chosenVideoSceneUse1)

        let video2 = SKVideoNode(url: chosenVideoSceneUse1!)
     
        video2.position = CGPoint(x: 90, y: -40)
        video2.zPosition = 1
        video2.xScale = 5
        video2.yScale = 5

        // below print statement outputs - *<SKVideoNode> name:'(null)' position:{90, -40} size:{0, 0} rotation:0.00*
        print(video2)
        addChild(video2)
        video2.play()

        //Plays but isnt ideal since pulling from project files instead of local director
        let urlStr = Bundle.main.path(forResource: "TryToHaveANiceDay-20210322", ofType: "mov")
        print(urlStr)
        let url = NSURL(fileURLWithPath: urlStr!)
        let player2 = AVPlayer(url: url as URL)
        let videoNode = SKVideoNode(avPlayer: player2)
        
        
        videoNode.position = CGPoint(x: 0, y: 0)
        videoNode.zPosition = 100
        videoNode.size = CGSize(width: 100, height: 100)
        videoNode.xScale = 1
        videoNode.yScale = 1

        addChild(videoNode)
        videoNode.play()
        

        // SKVideoNode displays but content does not
        let player = AVPlayer(url: chosenVideoSceneUse1! as URL)
        print(player)
        let video3 = SKVideoNode(avPlayer: player)

        video3.position = CGPoint(x: 0, y: 0)
        video3.zPosition = 100
        video3.size = CGSize(width: 100, height: 100)
        video3.xScale = 1
        video3.yScale = 1        

        addChild(video3)
        video3.play()

    //MARK: Override functions
    override func didMove(to view: SKView) {
        niceToMeetYou()
        
    }

    }

0

There are 0 answers