xcode 如何确保一次只播放一个视频?

deikduxw  于 2023-02-13  发布在  其他
关注(0)|答案(1)|浏览(124)

我创建了一个AR应用程序,可以在特定的图像锚点上播放视频。我想扩展该应用程序以处理多个图像。现在,当我扫描第一个图像时,它会播放视频,当我扫描第二个图像时,它会播放另一个视频。这里出现的问题是两个视频同时播放。
守则:

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {

    let node = SCNNode()
    
    if let imageAnchor = anchor as? ARImageAnchor {
        
        //below code needs to be duplicated in order to add more news
        
        if imageAnchor.referenceImage.name == "harrypotter" {
            
            let videoNode = SKVideoNode(fileNamed: "harrypotter.mp4")
            
            videoNode.play()
            
            let videoScene = SKScene(size: CGSize(width: 480, height: 360))
            
            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 = -.pi / 2
            
            node.addChildNode(planeNode)
            
        }

        if imageAnchor.referenceImage.name == "deatheater" {
         
         let videoNode = SKVideoNode(fileNamed: "deatheater.mp4")
            
            videoNode.play()
         
         let videoScene = SKScene(size: CGSize(width: 480, height: 360))
         
         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 = -.pi / 2
         
         node.addChildNode(planeNode)
         
     }

我尝试使用2个if条件,这有助于在特定图像锚点上播放视频。现在,当我扫描第一个图像时,它会播放视频,当我扫描第二个图像时,它会播放另一个视频。这里出现的问题是两个视频同时播放。我希望每当我播放另一个视频时,第一个视频都应自动停止。

des4xlb0

des4xlb01#

欢迎您来到比利时!
以下是一些建议,可帮助您获得所需内容并改进代码。

  • 创建一个函数来完成所有这些工作,而不是为每个图像复制代码。
  • 跟踪当前正在播放的歌曲,以便停止播放。

我不熟悉这些API,但您会看到以下内容:

class Player
{
    private let rootNode = SCNNode()
    private var playingNode: SCNNode?

    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor)
    {
        if let imageAnchor = anchor as? ARImageAnchor
        {
            if let newNode = playVideo(names: imageAnchor.referenceImage.name)
            {
                <inset code to stop playing 'playingNode' and remove it>

                playingNode = newNode
            }
        }
    }

    private func playVideo(named name: String) -> SCNNode?
    {
        guard <check if video file with 'name' exists> else { return nil }

        let videoNode = SKVideoNode(fileNamed: "\(name).mp4") 
        videoNode.play()
            
        let videoScene = SKScene(size: CGSize(width: 480, height: 360))
        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 = -.pi / 2

        return planeNode
    }
}

祝你好运。

相关问题