Changing the volume on SCNAudioPlayer

So, I have this little game I'm trying to build and inside it there are some pieces of wood that can slide on top of a bigger one.


I want to lower the "slide audio" volume based on the velocity of the wooden piece, but I can't seem to be able to change the output volume no matter what.

To play my sound I use an SCNAudioPlayer and attach it to the node like this:


let player = SCNAudioPlayer(source: source)           
player.willStartPlayback = {
     self.audioPlayersTracked[node] = player
}
player.didFinishPlayback = {
     self.audioPlayersTracked[node] = nil
}
node.addAudioPlayer(player)


Now everytime the physics is simulated I loop trough the "tracked" audio players and change it's volume based on the velocity of the sliding piece:


func renderer(renderer: SCNSceneRenderer, didSimulatePhysicsAtTime time: NSTimeInterval) {
     for (node, player) in audioPlayersTracked {
          var speed = velocityForNode(node)
          if speed < 0.01 {
               speed = 0.0
          }
          
          let volume = max(0.0, min(1.0, speed))
          player.audioNode?.engine?.mainMixerNode.volume = volume
          print("Did set volume \(volume) @ speed \(speed)")
     }
}


My logs show up alright. But the volume never changes. Any idea how is this supposed to be done?


P.S: The SCNAudioSource i init my player with has a volume property. But trying to change that doesn't work either. In fact for all properties of the SCNAudioSource the documentation mentions that if you need to change it "real-time" you should access the audioNode property of the SCNAudioPlayer

Replies

Hi zeusent,


Changing the mainMixerNode volume changes the global volume, not the one for the audio player. Have you tried changing the node's player volume instead? Each SCNAudioPlayer has an audioNode property that implements the AVAudioMixing protocol. Using AVAudioMixing's volume property on the audioNode should do the right thing.