当不再捕获检测到的图像时,停止播放视频

时间:2019-07-14 20:32:54

标签: ios swift xcode arkit

我正在创建一个AR应用程序以检测多维数据集上的图像(多维数据集上印有6张图像)。基于检测到的图像,正在播放相关视频。不幸的是,即使识别到另一张图片,AVplayer也不会停止播放视频->可能会同时播放多个视频(这对视频来说不是很糟糕,但重叠的音频尤其可怕)。

我的假设是,以下功能实际上将停止当前播放的视频,但是什么也没发生->即使多维数据集不再被摄像机捕获,音频也会继续。

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()
    //sceneView?.removeFromSuperview()
}

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

    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()
    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { (notification) in
        player.seek(to: CMTime.zero)
        player.play()

    }

    let videoScene = SKScene(size: CGSize(width: 960, height: 720))
    videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2)
    videoNode.yScale = -1.0
    videoScene.addChild(videoNode)
    let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
    plane.firstMaterial?.diffuse.contents = videoScene
    let planeNode = SCNNode(geometry: plane)
    planeNode.eulerAngles.x = -Float.pi / 2
    node.addChildNode(planeNode)
}

有人不知道如何调用“ player.pause()”或“ player.stop()”函数来在不再检测到捕获的图像时停止播放视频吗?

感谢您的支持!

1 个答案:

答案 0 :(得分:0)

对不起,我没有检查音频(以某种方式我的设备停止播放音频)。您有不同的AVPlayer实例为不同的图像节点播放视频。所以我只是将它们取出来,所以同一播放器将为不同节点提供新视频

var player = AVPlayer()
var videoScene: SKScene!
var videoNode: SKVideoNode!
var url: String!

func setupVideoNode(){
    videoNode = SKVideoNode(avPlayer: player)

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

    }
    videoScene = SKScene(size: CGSize(width: 960, height: 720))
    videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2)
    videoNode.yScale = -1.0
    videoScene.addChild(videoNode)
}

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

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

    let videoItem = AVPlayerItem(url: URL(fileURLWithPath: fileUrlString))

    player.replaceCurrentItem(with: videoItem) //= AVPlayer(playerItem: videoItem)

    setupVideoNode()

    let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
    plane.firstMaterial?.diffuse.contents = videoScene
    let planeNode = SCNNode(geometry: plane)
    planeNode.eulerAngles.x = -Float.pi / 2
    node.addChildNode(planeNode)
}

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
    guard let imageAnchor = anchor as? ARImageAnchor,
        let name = imageAnchor.referenceImage.name,
        let fileUrlString = Bundle.main.path(forResource: name, ofType: "mp4") else {return}

    if url == fileUrlString{
        return
    }
    url = fileUrlString
    let videoItem = AVPlayerItem(url: URL(fileURLWithPath: fileUrlString))
    player.replaceCurrentItem(with: videoItem)
    player.play()
}