SKVideoNode不显示视频

时间:2018-01-02 12:55:50

标签: ios video sprite-kit skvideonode

我一直试图将这些代码从Apple的文档中复制/粘贴,以播放本地视频,但似乎遗漏了一些东西。我已将AVFoundation和视频导入到项目中。我没有丢失任何错误,但无论我如何调整都无法看到任何视频(在我的设备或模拟器上)。我确定它很简单,但显然遗漏了一些东西。请帮忙!我正在运行最新版本的IOS和Xcode。

var videoNode: SKVideoNode? = {

        guard let urlString = Bundle.main.path(forResource: "Clouds", ofType: "mov") else {
            print("You messed up son")
            return nil
        }

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

        return SKVideoNode(avPlayer: player)
    }()


    override func didMove(to view: SKView) {

        addChild(worldNode)

        videoNode?.position = CGPoint( x: frame.midX, y: frame.midY)
        videoNode?.size.width = 1920
        videoNode?.size.height = 1080
        videoNode?.zPosition = 100
        worldNode.addChild(videoNode!)
        videoNode?.play()
    }

2 个答案:

答案 0 :(得分:0)

经过一段时间的搜索,我想我会尝试删除电影然后去构建阶段>复制捆绑资源。然后我在这个菜单中添加了我的电影,重建后它按预期工作。我猜我的电影副本最初可能有问题。无论如何,这可能会帮助有人在路上。

答案 1 :(得分:0)

步骤1在视图didload中配置设置照相机调用此方法

func setUpCamera ()
{
    sceneView.delegate = self
    // Do any additional setup after loading the view.
    let configuration = ARImageTrackingConfiguration()
    // first see if there is a folder called "ARImages" Resource Group in our Assets Folder
    if let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "ARImages", bundle: Bundle.main) {
        // if there is, set the images to track
        configuration.trackingImages = trackedImages
        // at any point in time, only 1 image will be tracked
        configuration.maximumNumberOfTrackedImages = 8
    }
    // Run the view's session
    sceneView.session.run(configuration,options: [.resetTracking, .removeExistingAnchors])

}

扫描图像时会调用此方法

// MARK: - ARSCNViewDelegate

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {



    DispatchQueue.main.async {
        self.btnFullScreen.isHidden = false
    }
    // if the anchor is not of type ARImageAnchor (which means image is not detected), just return
    guard let imageAnchor = anchor as? ARImageAnchor,
        let fileUrlString = Bundle.main.path(forResource: "videoplayback", ofType: "mp4") else {return}
    //find our video file
    let videoItem = AVPlayerItem(url: URL(fileURLWithPath: fileUrlString))
    player = AVPlayer(playerItem: videoItem)
    //initialize video node with avplayer
    let videoNode = SKVideoNode(avPlayer: player)
    player.play()
    // add observer when our player.currentItem finishes player, then start playing from the beginning
    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { (notification) in
        self.player.seek(to: CMTime.zero)
        self.player.pause()
        print("Looping Video")
    }
    // set the size (just a rough one will do)
    let videoScene = SKScene(size: CGSize(width: 480, height: 360))
    // center our video to the size of our video scene
    videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2)
    // invert our video so it does not look upside down
    videoNode.yScale = -1.0
    // add the video to our scene
    videoScene.addChild(videoNode)
    // create a plan that has the same real world height and width as our detected image
    let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
    // set the first materials content to be our video scene
    plane.firstMaterial?.diffuse.contents = videoScene
    // create a node out of the plane
    let planeNode = SCNNode(geometry: plane)
    // since the created node will be vertical, rotate it along the x axis to have it be horizontal or parallel to our detected image
    planeNode.eulerAngles.x = -Float.pi / 2
    // finally add the plane node (which contains the video node) to the added node
    node.addChildNode(planeNode)
}

enter image description here

这对我来说就像是魅​​力,如果您遇到任何困难,请告诉我,我会为您提供帮助

这项技术称为ARkitimage跟踪...