Changing the volume of an SCNAudioPlayer in real time - Swift

别说谁变了你拦得住时间么 提交于 2020-01-01 14:49:13

问题


I am trying to work out how I can changed the volume of an SCNAudioPlayer in real time.

Currently, I have an SCNAudioSource connected to an SCNAudioPlayer. This audio player is then assigned to a SCNNode so that my sound makes use of SceneKits spatial audio processing.

As it stands, I am able to change the volume of each SCNNode using SCNAudioSource.volume triggered by the boolean variable vol. An extract of my code for this is shown below:

(audioSource, audioCount) = soundFileSelect(audioFileIndex: 0) 
    audioSource?.isPositional = true 
    if vol {
        audioSource?.volume = 1.0
    }
    else {
        audioSource?.volume = 0.0
    }
    let audioPlayer = SCNAudioPlayer(source: audioSource!) 
    geometryNode.addAudioPlayer(audioPlayer) 
    let play = SCNAction.playAudio(audioSource!, waitForCompletion: true) 
    let loopPlay = SCNAction.repeatForever(play) 
    geometryNode.runAction(loopPlay) 

However, this only changes the default volume setting of the source, and so only happens when the node is spawned. I am trying to change the volume of it in real time, after a button press.

I feel I am missing something simple, and have trolled the internet for documentation and examples but am really struggling. From Apple's API documentation I am fairly certain it can be done. As far as I can tell you have to use the audioNode property associated with the SCNAudioPlayer which forms part of the AVAudioMixing protocol. Using this AVAudioMxing volume property should be what I need!

However, for the life of me I can't work out how to implement this process in code within my current setup. I can access mainMixer.volume/outputvolume such as:

audioPlayer.audioNode?.engine?.mainMixerNode.outputVolume=0

but this doesn't seem to work. How do the AV and SCN components link together?

Any help or examples of how one goes about implementing this would be hugely appreciated!

来源:https://stackoverflow.com/questions/43915630/changing-the-volume-of-an-scnaudioplayer-in-real-time-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!