如何在Swift中使用Arkit在图像上播放托管视频?

时间:2018-11-12 20:32:35

标签: ios swift augmented-reality arkit

当视频存储在本地时,我可以正常播放视频,但是现在我想播放已托管在Google驱动器上的视频。

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

        let configuration = ARImageTrackingConfiguration()

        if let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "TestImages", bundle: Bundle.main){
            configuration.trackingImages = trackedImages

            configuration.maximumNumberOfTrackedImages = 1
        }

        // Run the view's session
        ARView.session.run(configuration)
    }

这是我将视频呈现到图像上的功能:

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

        let node = SCNNode()

        if let imageAnchor = anchor as? ARImageAnchor{

            let videoNode = SKVideoNode(fileNamed: "Test.mp4")

            videoNode.play()

            let videoScene = SKScene(size: CGSize(width: 1080, height:720 ))

            videoScene.addChild(videoNode)

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

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

            videoNode.yScale = -1.0

            plane.firstMaterial?.diffuse.contents = videoScene

            let planeNode = SCNNode(geometry: plane)

            planeNode.eulerAngles.x = -.pi/2

            node.addChildNode(planeNode)


        }

        return node
    }

我是ARKIT的新手,所以我只学习基本功能并喜欢使用它们。

2 个答案:

答案 0 :(得分:1)

您的代码看起来还不错。您只需更改定义的videoNode

行即可

示例:

let videoNode = SKVideoNode(url: URL(fileURLWithPath: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"))

希望这会有所帮助!

答案 1 :(得分:-1)

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

    let configuration = ARImageTrackingConfiguration()

    if let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "TestImages", bundle: Bundle.main){
        configuration.trackingImages = trackedImages

        configuration.maximumNumberOfTrackedImages = 1
    }

    // Run the view's session
    ARView.session.run(configuration)
}