3

I have an AVPlayer used in SpriteKit as a material of SceneKit node. However, the AVPlayer will always auto start! I have tried to put the code in viewDidLoad or viewWillAppear (as some answers), but it still load automatically.

My code is something like this:

var apple1984Video:AVPlayer!

override func viewDidLoad() {
    super.viewDidLoad()
    ...
    apple1984Video = AVPlayer(url: Bundle.main.url(forResource: "apple1984", withExtension:"mp4")!)
    apple1984Video.actionAtItemEnd =.none
    apple1984Video.pause()

    let ad1 = addVideo(apple1984Video, name:"apple1984", position:SCNVector3(-0.1, -0.1, -0.5))
    scene.rootNode.addChildNode(ad1)
    ...
}

What can I do to stop the autoplay?

PS: here's the addVideo function but it is primary related to SceneKit/SpriteKit stuffs. I did not called play() in other parts of program.

func addVideo(_ player:AVPlayer, name:String, position:SCNVector3) -> SCNNode {
    let spriteKitScene = SKScene(size: CGSize(width: sceneView.frame.width, height: sceneView.frame.height))
    spriteKitScene.scaleMode = .aspectFill
    spriteKitScene.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)

    // Create the SpriteKit video node, containing the video player

    let videoNode = SKVideoNode(avPlayer: player)
    videoNode.position = CGPoint(x: spriteKitScene.size.width / 2.0, y: spriteKitScene.size.height / 2.0)
    videoNode.size = spriteKitScene.size
    videoNode.yScale = -1.0
    videoNode.alpha = 0.9
    spriteKitScene.addChild(videoNode)

    let geometry = SCNPlane(width: 0.4, height: 0.3)
    let node = SCNNode(geometry: geometry)

    geometry.firstMaterial?.isDoubleSided = true
    geometry.firstMaterial?.diffuse.contents = spriteKitScene

    node.name = name
    node.position = position
    player.pause()
    return node
}
Fluidity
  • 3,985
  • 1
  • 13
  • 34
Lim Thye Chean
  • 8,704
  • 9
  • 49
  • 88

0 Answers0