Search code examples
swiftxcode8delayavaudioplayer

Why calling audioplayer.play(atTime: delay) makes no sound regardless of the value of delay


Expect:

When audioplayer.play(atTime: 1) is called, a timer resets to 0, and the audioplayer is played at the 1st second

Reality:

I tried delay = 0.000000001, 1, 100000000, but regardless, no noise would ever be played. The code was clearly executed however (because "function was called" appeared in console)

Why the discrepancy?

C = AVAudioPlayer() // assume other setups are done
C.play(atTime: 1)
print("function was called")

Solution

  • according to the official API Reference (translation to swift 3 by me):

    Declaration

    func play(atTime time: TimeInterval) -> Bool
    

    Parameters

    time:

    The absolute audio output device time to begin playback. The value that you provide to the time parameter must be greater than the device’s current time. You can delay the start of playback by using code like this:

    let playbackDelay = 3.0              // must be ≥ 0
    myAudioPlayer.play(atTime: myAudioPlayer.deviceCurrentTime + playbackDelay)