AVAudioEngine seek the time of the song

前端 未结 2 1471
北荒
北荒 2021-02-06 12:33

I am playing a song using AVAudioPlayerNode and I am trying to control its time using a UISlider but I can\'t figure it out how to seek the time using

2条回答
  •  时光取名叫无心
    2021-02-06 13:09

    After MUCH trial and error I think I have finally figured this out.

    First you need to calculate the sample rate of your file. To do this get the last render time of your AudioNode:

    var nodetime: AVAudioTime  = self.playerNode.lastRenderTime
    var playerTime: AVAudioTime = self.playerNode.playerTimeForNodeTime(nodetime)
    var sampleRate = playerTime.sampleRate
    

    Then, multiply your sample rate by the new time in seconds. This will give you the exact frame of the song at which you want to start the player:

    var newsampletime = AVAudioFramePosition(sampleRate * Double(Slider.value))
    

    Next, you are going to want to calculate the amount of frames there are left in the audio file:

    var length = Float(songDuration!) - Slider.value
    var framestoplay = AVAudioFrameCount(Float(playerTime.sampleRate) * length)
    

    Finally, stop your node, schedule the new segment of audio, and start your node again!

    playerNode.stop()
    
    if framestoplay > 1000 {
       playerNode.scheduleSegment(audioFile, startingFrame: newsampletime, frameCount: framestoplay, atTime: nil,completionHandler: nil)
    }
    
    playerNode.play()
    

    If you need further explanation I wrote a short tutorial here: http://swiftexplained.com/?p=9

提交回复
热议问题