AVAudioPlayerNode在播放时更改播放时间

时间:2017-06-16 11:50:00

标签: swift uislider uitapgesturerecognizer avaudioplayernode

我有滑块,点按手势,playerNODE。当我触摸滑块上的某个位置时,我会使用滑动手势来获取滑块的位置。我想改变AVAudioPlayerNode播放时间。播放时间必须是滑块的位置。

func updateProgressBarWithGesture(_ sender: UITapGestureRecognizer) {
    songPlayer.pause()
    songRecorderPlayer.pause()
    videoPlayer.pause()
    let time = sender.location(in: sliderView).x / sliderView.frame.width
    songProgressSlider.value = Float(time)
    let totalTime = songRecorderPlayer.duration

    songPlayer.currentTime = TimeInterval(time * CGFloat(totalTime))
    songRecorderPlayer.currentTime = TimeInterval(time * CGFloat(totalTime))

    let second = time * CGFloat(songRecorderPlayer.duration)
    let videoTime = CMTime.init(value: CMTimeValue(second), timescale: 1)
    videoPlayer.seek(to: videoTime)
    print("                  ")
    print("Song Player: ",songPlayer.currentTime)
    print("Song Recorder Player: ",songRecorderPlayer.currentTime)
    print("Video Player: ",videoTime)

    if playerNODE.isPlaying{
        playerNODE.pause()
        playerNODE.reset()
        let seekTime = TimeInterval(time * CGFloat(totalTime))
        let startingFrame = seekTime * audioFile.processingFormat.sampleRate
        let frameCount: AVAudioFrameCount = (AVAudioFrameCount)(Double(audioFile.length) - startingFrame)
        playerNODE.scheduleSegment(audioFile, startingFrame: AVAudioFramePosition(startingFrame), frameCount: frameCount, at: nil, completionHandler: nil)
        print("Player Node: ",playerNODE.lastRenderTime!)
        playerNODE.play()
        songPlayer.play()
        videoPlayer.play()
    }

}

func updateProgressBar() {
    songProgressSlider.value = Float(songRecorderPlayer.currentTime/songRecorderPlayer.duration)
}

0 个答案:

没有答案