AVAudioPlayer fade volume out

前端 未结 13 2095
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 12:07

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

13条回答
  •  孤街浪徒
    2020-12-12 12:15

    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
            }
        }
    }
    

提交回复
热议问题