开发者社区> 问答> 正文

在ARKit中检测参考图像丢失的时间

我一直在开发一个应用程序来检测图像,一旦检测到,就会在上面显示视频。我从类似的项目中编译了很多这段代码,这些项目不太符合我的要求。我已经到了检测图像的程度,根据检测到的参考图像调用视频,最后在图像的顶部播放视频。这一切都很完美,我对结果非常满意,但是,我还没有能够完成一个看似相当基本的任务。当我将应用程序加载到测试设备上时,应用程序按预期工作,除非我将相机移离参考图像,否则在丢失检测到的图像后,它会继续播放背景中的音频(以及视频)。如果有人可以帮助暂停并重新启动视频,那么只能停留在暂停状态,直到再次检测到为止。所有东西都是从我的视图控制器运行的(粘贴在下面)

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!

    @IBOutlet var sceneView: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let audioSession = AVAudioSession.sharedInstance()

        do {
            try audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: .spokenAudio, options: .defaultToSpeaker)
            try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
        } catch {
            print("error.")
        }

        // Set the view's delegate
        sceneView.delegate = self

    }

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

        // Create a session configuration
        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 = 1
        }

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

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

        // Pause the view's session
        sceneView.session.pause()
    }

    // MARK: - ARSCNViewDelegate

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

        guard let imageAnchor = anchor as? ARImageAnchor else { return }

        node.addChildNode(createdVideoPlayerNodeFor(imageAnchor.referenceImage))

        let referenceImage = imageAnchor.referenceImage

    }

func createdVideoPlayerNodeFor(_ target: ARReferenceImage) -> SCNNode {
        let videoNode = SCNNode()

        let videoPlayerGeometry = SCNPlane(width: target.physicalSize.width, height: target.physicalSize.height)
        var player = AVPlayer()

        if let targetName = target.name,
            let fileUrlString = Bundle.main.url(forResource: targetName, withExtension: "mp4", subdirectory: "/art.scnassets") {
            player = AVPlayer(url: fileUrlString)
            player.play()

            NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { (notification) in
                        player.seek(to: CMTime.zero)
                        player.play()
            }



    }

    videoPlayerGeometry.firstMaterial?.diffuse.contents = player
    videoNode.geometry = videoPlayerGeometry
    videoNode.eulerAngles.x = -Float.pi / 2
    return videoNode

    override var prefersHomeIndicatorAutoHidden: Bool {
        return true
    }
}

感谢任何人和所有能帮我解决这个问题的人!

展开
收起
游客5akardh5cojhg 2019-12-11 21:34:38 439 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
复杂环境下的视觉同时定位与地图构建 立即下载
百度大规模时序指标自动异常检测实战 立即下载
低成本自动三维室内重建系统 立即下载