Swift: Trying to control time in AVAudioPlayerNode using UISlider

点点圈 提交于 2019-12-02 09:04:46

问题


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?

来源:https://stackoverflow.com/questions/56355698/swift-trying-to-control-time-in-avaudioplayernode-using-uislider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!