如何使用AVPlayer清洗音频?

7
我正在使用AVPlayer的seekToTime功能。它可以正常工作,但我希望能在视频滑动时听到音频,就像Final Cut或其他视频编辑器一样。只是想寻找一些想法或者是否有什么明显的遗漏。
1个回答

3
这可以通过在视频旁异步地同时使用AVplayer来完成。我是这样做的(使用Swift 4):
// create the simultaneous player and feed it the same URL:
    let videoPlayer2 = AVPlayer(url: sameUrlAsVideoPlayer!)
    videoPlayer2.volume = 5.0

//set the simultaneous player at exactly the same point as the video player.
     videoPlayer2.seek(to: sameSeekTimeAsVideoPlayer)

// create a variable(letsScrub)that allows you to activate the audio scrubber when the video is being scrubbed:
    var letsScrub: Bool?
//when you are scrubbing the video you will set letsScrub to true:
    if letsScrub == true {audioScrub()}

//create this function to scrub audio asynchronously:
    func audioScrub() {
        DispatchQueue.main.async {

//set the variable to false so the scrubbing does not get interrupted:
        self.letsScrub = false

//play the simultaneous player
        self.videoPlayer2.play()

//make sure that it plays for at least 0.25 of a second before it stops to get that scrubbing effect:
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {

//now that 1/4 of a second has passed (you can make it longer or shorter as you please) - pause the simultaneous player
            self.videoPlayer2.pause()

//now move the simultaneous player to the same point as the original videoplayer:
            self.videoPlayer2.seek(to: self.sameSeekTimeAsVideoPlayer)

//setting this variable back to true allows the process to be repeated as long as video is being scrubbed:
            self.letsScrub = true
        }
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接