Removing SKVideoNode does not stop the audio from the video node

238 views Asked by At

I've added an SKVideoNode to my GameScene. When I try to remove the video, the video image disappears, but the audio continues to play. I've tried to stop and pause the video before removing, but the audio continues to play regardless.

var introVideoIsPlaying = false
var introVideo: SKVideoNode!

func playIntroVideo() {

    introVideoIsPlaying = true

    if let videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4") {

        introVideo = SKVideoNode(url: videoURL)
        introVideo.position = CGPoint(x: frame.midX, y: frame.midY)
        introVideo.size = self.frame.size

        self.addChild(introVideo)
        introVideo.play()

    }
}

Then I remove the video in my touchesBegan -

if introVideoIsPlaying == true {

        introVideo.removeFromParent()
    }

What could I be missing? Is there a way to independently stop the audio in an SKVideoNode?

1

There are 1 answers

0
Kyle Porter On

It is better to use AVPlayer in connection with SKVideoNose like below:

(you will have more options to control the video)

var player: AVPlayer?
player = AVPlayer(url: videoURL)
let videoNode = SKVideoNode(avPlayer: player!)

You can then use the function didUpdate that fires anytime the the node changes. You can pause the video when the node is out of view like below

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
    if node.isHidden == true {
        print("Node is out of view")
        self.player?.pause()

    }

}