Swift: Trying to control time in AVAudioPlayerNode

2020-04-20 05:55发布

问题:

I'm using an AVAudioPlayerNode attached to an AVAudioEngine to play a sound.

to get the current time of the player I'm doing this:

extension AVAudioPlayerNode {
var currentTime: TimeInterval {
    get {
        if let nodeTime: AVAudioTime = self.lastRenderTime, let playerTime: AVAudioTime = self.playerTime(forNodeTime: nodeTime) {
            return Double(playerTime.sampleTime) / playerTime.sampleRate
        }
        return 0
    }
 }

}

I have a slider that indicates the current time of the audio. When the user changes the slider value, on .ended event I have to change the current time of the player to that indicated in the slider. To do so:

extension AVAudioPlayerNode {

func seekTo(value: Float, audioFile: AVAudioFile, duration: Float) {
    if let nodetime = self.lastRenderTime{
        let playerTime: AVAudioTime = self.playerTime(forNodeTime: nodetime)!
        let sampleRate = self.outputFormat(forBus: 0).sampleRate
        let newsampletime = AVAudioFramePosition(Int(sampleRate * Double(value)))
        let length = duration - value
        let framestoplay = AVAudioFrameCount(Float(playerTime.sampleRate) * length)
        self.stop()

        if framestoplay > 1000 {
            self.scheduleSegment(audioFile, startingFrame: newsampletime, frameCount: framestoplay, at: nil,completionHandler: nil)
        }
    }
    self.play()
}

However, my function seekTo is not working correctly(I'm printing currentTime before and after the function and it shows always a negative value ~= -0.02). What is the wrong thing I'm doing and can I find a simpler way to change the currentTime of the player?

回答1:

I ran into same issue. Apparently the framestoplay was always 0, which happened because of sampleRate. The value for playerTime.sampleRate was always 0 in my case.

So,

let framestoplay = AVAudioFrameCount(Float(playerTime.sampleRate) * length)

must be replaced with

let framestoplay = AVAudioFrameCount(Float(sampleRate) * length)