如何通过 AVPlayer 以 2.0 速率播放带音频的视频 iOS
How to play video at rate 2.0 with audio via AVPlayer iOS
我正在 iOS 上制作视频播放器。我想要以 2.0 播放速度播放视频的功能。但是当我设置rate 2.0时,视频可以播放但是这里没有声音。请帮助我并告诉我我做错了什么。下面是我的代码:
let url = URL(string: self.url)
let asset = AVAsset(url: url!)
if (!asset.isPlayable) {
print (self.url + " cannot to play.")
return
}
let playItem = AVPlayerItem(asset: asset)
playItem.audioTimePitchAlgorithm = AVAudioTimePitchAlgorithm.lowQualityZeroLatency
self.player = MyAVPlayer(playerItem: playItem)
self.playerViewController = AVPlayerViewController()
self.playerViewController?.player = self.player
self.player?.allowsExternalPlayback = true
// self.playerViewController?.showsPlaybackControls = self.showControl
if (self.autoPlay) {
// player?.play()
player?.play()
player?.rate = 2.0
}
经过一个月的研究,我找到了解决方案。其实很简单,把2.0换成1.99。
我认为 swift 将 2.0 转换为浮点值时会出现问题,转换后的值可能是 2.00000000003,大于 2.0 并且它不起作用。
我正在 iOS 上制作视频播放器。我想要以 2.0 播放速度播放视频的功能。但是当我设置rate 2.0时,视频可以播放但是这里没有声音。请帮助我并告诉我我做错了什么。下面是我的代码:
let url = URL(string: self.url)
let asset = AVAsset(url: url!)
if (!asset.isPlayable) {
print (self.url + " cannot to play.")
return
}
let playItem = AVPlayerItem(asset: asset)
playItem.audioTimePitchAlgorithm = AVAudioTimePitchAlgorithm.lowQualityZeroLatency
self.player = MyAVPlayer(playerItem: playItem)
self.playerViewController = AVPlayerViewController()
self.playerViewController?.player = self.player
self.player?.allowsExternalPlayback = true
// self.playerViewController?.showsPlaybackControls = self.showControl
if (self.autoPlay) {
// player?.play()
player?.play()
player?.rate = 2.0
}
经过一个月的研究,我找到了解决方案。其实很简单,把2.0换成1.99。 我认为 swift 将 2.0 转换为浮点值时会出现问题,转换后的值可能是 2.00000000003,大于 2.0 并且它不起作用。