1

我有滑块,点击手势,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)
}
4

0 回答 0