使用AVPlayer对象初始化SKVideoNode-SWIFT

问题描述 投票:0回答:1

我正在使用ARKit,并创建了一个将视频内容叠加到可识别图像上的应用程序。代码如下:

import UIKit
import SceneKit
import ARKit
import SpriteKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()

        sceneView.delegate = self

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let configuration = ARImageTrackingConfiguration()

        if let imageToTrack = ARReferenceImage.referenceImages(inGroupNamed: "ALLimages", bundle: Bundle.main) {

            configuration.trackingImages = imageToTrack

            configuration.maximumNumberOfTrackedImages = 3

            print("Images successfully added")

        }

        sceneView.session.run(configuration)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        sceneView.session.pause()
    }

    // MARK: - ARSCNViewDelegate

    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        let node = SCNNode()

        var videoNode = SKVideoNode()

        var videoScene = SKScene()

        if let imageAnchor = anchor as? ARImageAnchor {


            videoNode = SKVideoNode(fileNamed: "video1.mp4")

            videoNode.play()

            videoScene = SKScene(size: CGSize(width: 640, height: 360))

            videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2)

            videoNode.yScale = -1.0

            videoScene.addChild(videoNode)

            let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)

             plane.firstMaterial?.diffuse.contents = videoScene

            let planeNode = SCNNode(geometry: plane)

            planeNode.eulerAngles.x = -.pi/2

            node.addChildNode(planeNode)

        }
        return node
    }
}

这很完美...一次!但是问题在于,一旦视频播放完毕,由于可通过SKVideoNode使用的控件有限,我无法弄清楚如何自动重新启动视频。理想情况下,这些应该循环播放。

我已经进行了一些研究,似乎最好的方法是使用AVPlayer对象初始化视频节点。

因此,我尝试执行此操作,但无法使其正常工作。

我在类中添加了var player = AVPlayer(),然后尝试如下初始化我的videoNode:

var videoNode: SKVideoNode? = {
    guard let urlString = Bundle.main.path(forResource: "video1", ofType: "mov") else {
        return nil
    }

    let url = URL(fileURLWithPath: urlString)
    let item = AVPlayerItem(url: url)
    let player = AVPlayer(playerItem: item)

    return SKVideoNode(avPlayer: player)
}()

然后我尝试使用player.play(),但视频无法播放。相反,我的飞机只是在图像上显示为空白矩形。

一旦成功初始化它,我想我可以添加一个观察者来检查视频何时结束了播放器并重新启动它,但是我在努力达到这一点。

ios swift avplayer skvideonode
1个回答
0
投票

首先,您不需要SCNNode的SKScene中的SKVideoNode。您可以直接将AVPlayer用作SCNNode的分散内容:plane.firstMaterial?.diffuse.contents = player

要进行循环播放,您必须订阅播放器上的通知,并在结束时将时间重置为零:

NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { _ in
    player.seek(to: .zero)
    player.play()
}
© www.soinside.com 2019 - 2024. All rights reserved.