1

我一直试图让这段代码几乎从 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()
    }
4

2 回答 2

0

在搜索了一段时间后,我想我会尝试删除电影,然后转到 Build Phases > Copy Bundle Resources。然后我在这个菜单中添加了我的电影,重建后它按预期工作。我猜我最初将电影复制到项目中可能有问题。无论如何,也许这会帮助别人。

于 2018-01-02T21:51:53.003 回答
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)
}

在此处输入图像描述

这对我来说就像魅力一样工作让我知道如果你发现任何困难我会帮助你

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

于 2020-03-13T05:52:01.243 回答