【问题标题】:SCNAudioSource won't play with a SCNNodeSCNAudioSource 不会与 SCNNode 一起播放
【发布时间】:2018-09-24 11:06:43
【问题描述】:

每次点击屏幕时,我都会尝试播放声音。这是我的代码

全球:

var shapeNode: SCNNode!
var SoundAction: SCNAction!

ViewDidLoad:

    let audioSource = SCNAudioSource(named: "launch.mp3")!
    audioSource.isPositional = true
    audioSource.volume = 1.0
    SoundAction = SCNAction.playAudio(audioSource, waitForCompletion: false)

@objc func sceneTapped(识别器:UITapGestureRecognizer)

    shapeNode = SCNNode(geometry: myshape)

    self.myscene?.rootNode.addChildNode(shapeNode)
    shapeNode.runAction(SoundAction)

当我触摸屏幕时不会播放声音...请帮忙

【问题讨论】:

    标签: ios swift xcode scenekit scnnode


    【解决方案1】:

    我生成了一个默认游戏项目,然后输入您的代码。起初,我收到了相同的结果。然而,当我取了 let scene = SCNScene(named: "art.scanassets/ship.scn")!并将场景设置为类变量,播放声音。或者,如果您在加载过程中添加 SoundNode 而不是在点击时添加,声音也会播放。

    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)
        }
    

    【讨论】:

      猜你喜欢
      • 2013-05-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2015-08-07
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      相关资源
      最近更新 更多