AR应用程序可根据ARImageAnchor播放不同的视频

时间:2019-07-09 08:15:06

标签: ios swift xcode augmented-reality arkit

我正在为iOS创建一个AR应用程序,以检测多维数据集上的图像(-> 6个要识别的不同图像)。一旦检测到一个图像,就应该播放视频-每个不同的图像都应播放不同的视频(对于图像1,应该播放视频1 ...)。

我的问题是我不知道如何跟踪当前正在跟踪的图像以及基于哪个视频播放

现在,该应用跟踪不同的图像,但始终播放相同的视频(视频“ 1” .mp4)

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let configuration = ARImageTrackingConfiguration()

    if let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "ARImages", bundle: Bundle.main) {
        configuration.trackingImages = trackedImages
        configuration.maximumNumberOfTrackedImages = 6
    }
    sceneView.session.run(configuration)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    sceneView.session.pause()
}

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

    guard let imageAnchor = anchor as? ARImageAnchor, 
          let fileUrlString = Bundle.main.path(forResource: "1", ofType: "mp4") else {
        return
    }
    let videoItem = AVPlayerItem(url: URL(fileURLWithPath: fileUrlString))
    let player = AVPlayer(playerItem: videoItem)
    let videoNode = SKVideoNode(avPlayer: player)

    player.play()
    ...
}

如何识别当前的ImageAnchor并链接相关视频?

1 个答案:

答案 0 :(得分:1)

您可以给ARReferenceImage命名,所以一种方法是为图像和关联的视频文件赋予相同的名称。

guard let imageAnchor = anchor as? ARImageAnchor,
             let name = imageAnchor.referenceImage.name,
             let fileUrlString = Bundle.main.path(forResource: name, ofType: "mp4") else {
    return
}

let videoItem = AVPlayerItem(url: URL(fileURLWithPath: fileUrlString))
let player = AVPlayer(playerItem: videoItem)
let videoNode = SKVideoNode(avPlayer: player)

player.play()