title: AVAudioPlayerNode volume change is not applied immediately
body: <p>Using AVFoundation and AVAudioPlayerNode to play sound with Xcode Version 9.2 (9C40b) and deploying to iOS 11.2</p>
  
  <p>The problem is that when you change volume, the change is not applied the first time you play the sound, and there are other weird effects.</p>
  
  <p>Start a new project in Xcode, select iOS Game, and give any name. Then replace the code in GameScene.swift with:</p>
  
  <pre><code>import SpriteKit
  import GameplayKit
  import AVFoundation
  
  class GameScene: SKScene {
  
      var entities = [GKEntity]()
      var graphs = [String : GKGraph]()
  
      let audioFilePlayer = AVAudioPlayerNode()
      var audioFile:AVAudioFile! = nil
      var audioFileBuffer:AVAudioPCMBuffer! = nil
  
      override func sceneDidLoad() {
          do {
              let path = Bundle.main.path(forResource: "sound", ofType: "caf")!
              let url = URL(fileURLWithPath: path)
              audioFile = try AVAudioFile(forReading: url)
              audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFile.processingFormat, frameCapacity: UInt32(audioFile.length))
              try audioFile.read(into: audioFileBuffer!)
              audioEngine.attach(audioFilePlayer)
              audioEngine.connect(audioFilePlayer, to: audioEngine.mainMixerNode, format: audioFileBuffer?.format)
              try audioEngine.start()
          }
          catch {
              print(error)
          }
  
          let playAction = SKAction.sequence([
              SKAction.wait(forDuration: 3.0),
              SKAction.run { self.play(volume: 1.0) },
              SKAction.wait(forDuration: 1.0),
              SKAction.run { self.play(volume: 1.0) },
              SKAction.wait(forDuration: 1.0),
              SKAction.run { self.play(volume: 0.0) },
              SKAction.wait(forDuration: 1.0),
              SKAction.run { self.play(volume: 0.0) },
              SKAction.wait(forDuration: 1.0),
              SKAction.run { self.play(volume: 1.0) },
              SKAction.wait(forDuration: 1.0),
              SKAction.run { self.play(volume: 1.0) },
              ])
          self.run(playAction)
  
      }
  
      func play(volume:Float) {
          print("playing at \(volume)")
          audioFilePlayer.stop()
          audioFilePlayer.volume = volume
          audioFilePlayer.scheduleFile(audioFile, at: nil, completionHandler: nil)
          audioFilePlayer.play()
      }
  
  }
  </code></pre>
  
  <p>Apologies for poor optionals... Also, add a sound file called sound.caf to the project.</p>
  
  <p>Console output is:</p>
  
  <pre><code>playing at 1.0
  playing at 1.0
  playing at 0.0
  playing at 0.0
  playing at 1.0
  playing at 1.0
  </code></pre>
  
  <p>I would expect to hear: loud, loud, nothing, nothing, loud, loud.</p>
  
  <p>I am actually hearing: loud, loud, loud, nothing, soft, loud</p>
  
  <p>(the 'soft' is particularly weird)</p>
  
  <p>I have also tried changing the master volume with:</p>
  
  <pre><code>audioEngine.mainMixerNode.outputVolume = volume
  </code></pre>
  
  <p>but the sounds are the same.</p>
  
  <p>In older games, used to use OpenAL, but this requires Obj-C and is pretty messy with bridging headers etc. Also, OpenAL is supposedly not supported any more. SKAction based audio cannot handle lots of sounds repeating fast without glitches and scratches (it's a space shooter game...) Problem is the same in simulator and on device. Any help appreciated!</p>