问题描述
我正在尝试研究如何实时更改SCNAudioPlayer
的音量.
I am trying to work out how I can changed the volume of an SCNAudioPlayer
in real time.
当前,我有一个SCNAudioSource
连接到SCNAudioPlayer
.然后将此音频播放器分配给SCNNode
,以便我的声音使用SceneKits的空间音频处理.
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.
就目前而言,我可以使用由布尔变量vol
触发的SCNAudioSource.volume
来更改每个SCNNode
的音量.我的代码摘录如下:
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.
我觉得我缺少一些简单的东西,并且已经在互联网上寻找文档和示例,但确实很挣扎.从Apple的API文档中,我相当确定它可以完成.据我所知,您必须使用与SCNAudioPlayer
关联的audioNode
属性,该属性构成AVAudioMixing
协议的一部分.使用此AVAudioMxing
卷属性应该是我所需要的!
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!
但是,对于我一生来说,我无法弄清楚如何在当前设置中用代码实现此过程.我可以访问mainMixer.volume
/outputvolume
,例如:
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
但是这似乎不起作用. AV和SCN组件如何链接在一起?
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!
推荐答案
guard let avNode = playerGuitar?.audioNode as? AVAudioMixing else {
return
}
avNode.volume = volume
在我的代码中工作(虽然花了很多年才发现,但是迅速熟悉并没有完全帮助). playerGuitar是一个SCNAudioPlayer变量.
worked in my code (took ages to find though, and being new to swift didn't exactly help). playerGuitar is a SCNAudioPlayer variable.
这篇关于实时更改SCNAudioPlayer的音量-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!