0

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

    }

Ojay
  • 61
  • 12
  • Before 'let video2 .. ' add this 'let url2 = URL(fileURLWithPath: chosenVideoSceneUse1!)', then change video2 to 'let video2 = SKVideoNode(url: url2)'. Alternatively, could you change the zPosition to 100 instead of 1. – JohnL Apr 16 '21 at 14:18
  • @JohnL Thanks! but for the url2 set, it says cannot convert value of type URL to expected argument type String... – Ojay Apr 16 '21 at 15:39
  • Ok, ignore the url2 then. I can run your code in func videoPlay if i replace chosenVideoSceneUse1 with my own url, the video displays with sound. You need to show how you define chosenVideoSceneUse1 then. – JohnL Apr 16 '21 at 15:55
  • @JohnL chosenVideoSceneUse1 is of type URL, which includes this reference: `*Optional(file:///private/var/mobile/Containers/Data/Application/3D99929D-CFC4-418A-B215-3AE463F3AD38/tmp/.com.apple.Foundation.NSItemProvider.WMPrvs/RPReplay_Final1617465667.mp4)*` The original variable is set via another vc, which is accessed through my tabcontroller. The video plays fine if i use SKVideoNode(fileNamed:) but i want the user to be able to use videos from their library. – Ojay Apr 16 '21 at 16:12
  • @JohnL how are you inputting the url? Are you using raw text? If that's the case, its a string and not a url. – Ojay Apr 26 '21 at 13:09
  • @Ojay move your `AVPlayer` declaration out of your method https://stackoverflow.com/a/30988140/2303865 – Leo Dabus Apr 29 '21 at 13:37
  • @LeoDabus thanks but didn't work. The issue has something to do with the url, if i use this method `let urlStr = Bundle.main.path(forResource: "TryToHaveANiceDay-20210322", ofType: "mov")` it works fine, however when i use this method, `let video2 = SKVideoNode(url: chosenVideoSceneUse1!)` it doesnt work. The video2 method is receiving a url from my phpicker method. I believe it has something to do with photo library privacy but can't seem to find a workable solution. – Ojay Apr 30 '21 at 18:26
  • You are probably trying to open an asset URL. This might help https://stackoverflow.com/a/43944769/2303865 – Leo Dabus Apr 30 '21 at 18:42
  • @LeoDabus thanks for the lead, ill dive into it. It's tough because Apple deprecated UIImagePicker and now you have to use PHPicker: [https://developer.apple.com/documentation/photokit/phpickerviewcontroller] – Ojay Apr 30 '21 at 19:23
  • @LeoDabus i put a breakpoint right after the video2.play(), in the debugger, next to my property housing the url, it says ("URL?) Failed to get the 'some' field from optional chosenVideoSceneUse1" do you know what that means? I tried searching but no luck. – Ojay May 05 '21 at 02:06

0 Answers0