【问题标题】:Audio not playing under scenekit在场景包下无法播放音频
【发布时间】:2021-08-15 16:06:39
【问题描述】:

如何在scenekit下播放音频?

let source = SCNAudioSource(fileNamed: "sample.mp3")
let action = SCNAction.playAudio(source!, waitForCompletion: true)
        
let scene = SCNScene()
self.scenekitView.scene = scene
scene.rootNode.runAction(action)  // no sound

【问题讨论】:

    标签: ios swift audio scenekit


    【解决方案1】:

    尝试将 'let scene' 更改为 var 场景,然后在 viewDidLoad 中处理其余部分,如下所示:

    class GameViewController: UIViewController {
    
        var SoundAction = SCNAction()
        var SoundNode = SCNNode()
        var scene = SCNScene()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Change this
            scene = SCNScene(named: "art.scnassets/ship.scn")!
    
            let cameraNode = SCNNode()
            cameraNode.camera = SCNCamera()
            scene.rootNode.addChildNode(cameraNode)
            cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
    
            let ship = scene.rootNode.childNode(withName: "ship", recursively: true)!
            ship.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 2, z: 0, duration: 1)))
    
            let audioSource = SCNAudioSource(named: "MenuWinner.caf")!
            audioSource.volume = 1.0
            SoundAction = SCNAction.playAudio(audioSource, waitForCompletion: false)
    
            let scnView = self.view as! SCNView
            scnView.scene = scene
            scnView.allowsCameraControl = true
            scnView.showsStatistics = true
            scnView.backgroundColor = UIColor.black
    
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
            scnView.addGestureRecognizer(tapGesture)
        }
    
        @objc
        func handleTap(_ gestureRecognize: UIGestureRecognizer) {
            scene.rootNode.addChildNode(SoundNode)
            SoundNode.runAction(SoundAction)
        }
    

    【讨论】:

      【解决方案2】:

      source.load() 修复了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 2018-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多