I have an AVAudioPlayer playing some audio (duh!)
The audio is initiated when the user presses a button. When they release it I want the audio to fade out.
I
An extension for swift 3 inspired from the most voted answer. For those of you who like copy-pasting :)
extension AVAudioPlayer {
func fadeOut() {
if volume > 0.1 {
// Fade
volume -= 0.1
perform(#selector(fadeOut), with: nil, afterDelay: 0.1)
} else {
// Stop and get the sound ready for playing again
stop()
prepareToPlay()
volume = 1
}
}
}