简体   繁体   中英

How can I pause SCNAudioPlayer playback in ARKit?

I'm trying to implement a play/pause button for positional audio attached to a node in an ARKit scene. I want the user to be able to pause the audio, then resume it from the paused point at a later time. So removeAudioPlayer() is no good, as this resets the audio back to the start when it is reattached.

The only pause method associated with SCNAudioPlayers that I can find is buried deep down in:

myAudioPlayer.audioNode?.engine?.pause()

But this doesn't seem to have any effect. Strangely

myAudioPlayer.audioNode?.engine?.stop()

Does stop the audio from playing, but then you can't start it again afterwards!

My code for creating the audioPlayer is as follows:

audioSource = SCNAudioSource(fileNamed: "audio.caf")!
audioSource.loops = false
audioSource.isPositional = true
audioSource.shouldStream = false
audioSource.load()
// then a bit later...
audioPlayer = SCNAudioPlayer(source: audioSource)
myARNode.addAudioPlayer(audioPlayer)

Notice that no 'play' method is needed. The audio just starts playing as soon as you attach it to the node. And the lack of straight play() and pause() methods as top-level properties of SCNAudioPlayer makes me wonder if it's possible at all. This seems slightly crazy however, as it's a pretty rudimentary aspect of working with audio.

Does anyone know if this can be done?

Thanks in advance.

To stop audio engine use 2 sec delay. Pause doesn't work here (I'm using Xcode 12.2).

DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
    player.audioNode?.engine?.stop()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM