0

I am currently trying to play a video in a loop via SpriteKit. I know that I need AVFoundation and the AVPLayer() for this, however I don't know how to initialize and use this... Could someone help me with this?

Here is my current code block:

let mapSeriallyVideo = SKVideoNode(fileNamed: "map_serially.mp4")
                mapSeriallyVideo.position = CGPoint(x: 850, y: 1100)
                addChild(mapSeriallyVideo)
                mapSeriallyVideo.play()
chickenbox
  • 25
  • 6
  • That should work. Make sure in xcode, when you click on the actual video file "map_serially.mp4" that the Target Membership is ticked on the right hand side. – JohnL Apr 11 '21 at 10:09
  • With the code above, the video is unfortunately only played once. Hence the approach with the AVPlayer. What will happen if i tick it on the right hand side? I don't know what you mean by Target Membership. I am relatively new to SpriteKit. – chickenbox Apr 11 '21 at 10:35
  • Ah i see, sorry, i thought the video wouldn't play for you. If it runs at least once then the Target Membership tick box is ticked then. If you actually click on the video file in xcode, Identity and Type inspector menu appears, it was an option in there. I'll have to think about the AVPlayer() implementation. – JohnL Apr 11 '21 at 10:49
  • All right. Thanks! – chickenbox Apr 11 '21 at 15:01
  • If you can create an `SKVideoNode` with `AVPlayer` then you should be able to create it with an `AVQueuePlayer` + `AVPlayerLooper` as in this code sample: https://developer.apple.com/documentation/avfoundation/avplayerlooper – Rhythmic Fistman Apr 11 '21 at 15:18
  • Oh perfect. I think this is what I need. How can I declare an AVAsset correctly in the first line? `let asset = // AVAsset with its 'duration' property value loaded` – chickenbox Apr 11 '21 at 17:16
  • Answered here: [play video forever in sprite kit](https://stackoverflow.com/a/73124473/3050403) – kelin Jul 26 '22 at 13:53

0 Answers0