audioscenekitarkitarscnview

ARKit SceneKit ARSCNView with Positional Audio SCNAudioPlayer WILL NOT STOP playing


Running ARKit 2.0 with an ARSCNView. iOS12 The application uses multithreading, that's why these functions are being performed on the main thread (just to be sure). I also tried without explicitly performing the functions on the main thread too, with no avail.

I'm using an .aiff sound file but have also tried a .wav. No joy.

I even tried removing audioNode_alarm from the node hierarchy & the sound still plays. I even remove the ARSCNView from the view hierarchy and the sound STILL plays. FFS

From what I can see, I'm doing things EXACTLY as I'm supposed to, to stop the audio from playing. The audio simply will not stop no matter what I try. Can anyone think why?!

weak var audioNode_alarm: SCNNode!
weak var audioPlayer_alarm: SCNAudioPlayer?

func setupAudioNode() {

    let audioNode_alarm = SCNNode()
    addChildNode(audioNode_alarm)
    self.audioNode_alarm = audioNode_alarm

}

func playAlarm() {

    DispatchQueue.main.async { [unowned self] in

        self.audioNode_alarm.removeAllAudioPlayers()

        if let audioSource_alarm = SCNAudioSource(fileNamed: "PATH_TO_MY_ALARM_SOUND.aiff") {

            audioSource_alarm.loops = true
            audioSource_alarm.load()
            audioSource_alarm.isPositional = true
            let audioPlayer_alarm = SCNAudioPlayer(source: audioSource_alarm)
            self.audioNode_alarm.addAudioPlayer(audioPlayer_alarm)
            self.audioPlayer_alarm = audioPlayer_alarm

        }

    }

}

func stopAlarm() {

    DispatchQueue.main.async { [unowned self] in

        self.audioNode_alarm?.removeAudioPlayer(self.audioPlayer_alarm!)
        self.audioNode_alarm?.removeAllAudioPlayers()

    }

}

Solution

  • What I ended up doing is stopping the sound and removing the player by

    yourNode.audioPlayers.forEach { audioLocalPlayer in
      audioLocalPlayer.audioNode?.engine?.stop()
      yourNode.removeAudioPlayer(audioLocalPlayer)
    }
    

    According to the documentation SCNAudioPlayer has audioNode, which is supposed to be used "to vary parameters such as volume and reverb in real time during playback".

    audioNode is of AVAudioNode type, so if we jump to engine prop and its type definition, we'll find all the controls we need.