SceneKit –离开时位置音频不会淡出

问题描述 投票:1回答:1

我目前正在为要开发的游戏制作一个关卡,并已将音频添加到游戏中的壁炉SCNNode。由于某些原因,当我带着角色离开壁炉时,音频不会消失。音频文件是mono

 let audioSource = SCNAudioSource(fileNamed: "art.scnassets/Scenes/fireplace.wav")!
 audioSource.loops = true
 audioSource.volume = 1
 audioSource.isPositional = true
 audioSource.shouldStream = false
 audioSource.load()
 node.addAudioPlayer(SCNAudioPlayer(source: audioSource))

我还需要做其他任何事情才能使位置音频正常工作,以便随着我离开壁炉的距离变远而逐渐消失?

ios swift scenekit augmented-reality arkit
1个回答
0
投票

您在代码中使用的设置还不够。 位置声音的要点是将AVAudioMixing协议及其存根使用。但是,还有其他设置:

  • mySource.load()放在.addAudioPlayer()方法之后
  • 使用maximumDistance实例属性(用于距离控制)
  • 使用referenceDistance实例属性(用于距离控制)

在macOS中尝试此工作代码,然后然后使其适应iOS需求

import SceneKit
import AVFoundation

@available(OSX 10.15, *)
class GameViewController: NSViewController, AVAudioMixing, SCNSceneRendererDelegate {

    func destination(forMixer mixer: AVAudioNode,
                                bus: AVAudioNodeBus) -> AVAudioMixingDestination? {
        return nil
    }

    var volume: Float = 0.05
    var pan: Float = 0.0

    var sourceMode: AVAudio3DMixingSourceMode = .bypass
    var pointSourceInHeadMode: AVAudio3DMixingPointSourceInHeadMode = .bypass
    @IBOutlet var sceneView: SCNView!

    var renderingAlgorithm = AVAudio3DMixingRenderingAlgorithm.sphericalHead
    var rate: Float = 1.2
    var reverbBlend: Float = 40.0
    var obstruction: Float = -100.0
    var occlusion: Float = -100.0
    var position = AVAudio3DPoint(x: 0, y: 0, z: 0.2)
    let audioNode = SCNNode()

    override func viewDidLoad() {
        super.viewDidLoad()

        let myScene = SCNScene()
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.camera?.zNear = 0
        cameraNode.position = SCNVector3(0, 0, 5)
        myScene.rootNode.addChildNode(cameraNode)

        let sceneView = view as! SCNView
        sceneView.delegate = self
        sceneView.scene = myScene
        sceneView.backgroundColor = NSColor.orange

        let myPath = Bundle.main.path(forResource: "Mono_Audio", ofType: "mp3")

        let myURL = URL(fileURLWithPath: myPath!)
        let mySource = SCNAudioSource(url: myURL)!
        mySource.loops = true
        mySource.isPositional = true   // Positional Audio
        mySource.shouldStream = false  // FALSE for Positional Audio
        mySource.volume = volume
        mySource.reverbBlend = reverbBlend
        mySource.rate = rate

        let player = SCNAudioPlayer(source: mySource)
        let sphere: SCNGeometry = SCNSphere(radius: 0.05)
        let sphereNode = SCNNode(geometry: sphere)
        sphereNode.addChildNode(audioNode)
        myScene.rootNode.addChildNode(sphereNode)
        audioNode.addAudioPlayer(player)

        sceneView.audioEnvironmentNode.distanceAttenuationParameters.maximumDistance = 2
        sceneView.audioEnvironmentNode.distanceAttenuationParameters.referenceDistance = 0.1
        sceneView.audioEnvironmentNode.renderingAlgorithm = .auto

        mySource.load()

        let hither = SCNAction.moveBy(x: 0, y: 0, z: 10, duration: 4)
        let thither = SCNAction.moveBy(x: 0, y: 0, z: -10, duration: 4)         
        let sequence = SCNAction.sequence([hither, thither])
        let loop = SCNAction.repeatForever(sequence)
        sphereNode.runAction(loop)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.