3

我有一个在 SpriteKit 中使用的 AVPlayer 作为 SceneKit 节点的材质。但是,AVPlayer 将始终自动启动!我试图将代码放在 viewDidLoad 或 viewWillAppear 中(作为一些答案),但它仍然会自动加载。

我的代码是这样的:

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

我该怎么做才能停止自动播放?

PS:这是 addVideo 函数,但它主要与 SceneKit/SpriteKit 相关。我没有在程序的其他部分调用 play() 。

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
}
4

0 回答 0