gpt4 book ai didi

avplayer - 具有缓冲进度的 UISlider - AVPlayer

转载 作者:行者123 更新时间:2023-12-05 07:30:46 32 4
gpt4 key购买 nike

enter image description here我正在使用 AVPlayer 从服务器流式传输音频,我现在想做的是显示一个显示缓冲进度的自定义 UISlider。

有人想出这个吗?请给我解决方案。现在播放器一次加载整个持续时间并在 UISlider 中显示。

最佳答案

你需要使用preferredforwardbufferduration

This property defines the preferred forward buffer duration in seconds. If set to 0, the player will choose an appropriate level of buffering for most use cases. Setting this property to a low value will increase the chance that playback will stall and re-buffer, while setting it to a high value will increase demand on system resources.

还可以在此处查看有关如何使用的详细示例:Is it possible to pause and resume buffering of AVPlayer?

在您的播放器中,您需要使用以下函数观察时间范围

覆盖 func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)

像这样:

            let timeRanges = change![NSKeyValueChangeKey.newKey] as? NSArray
if timeRanges != nil && timeRanges!.count != 0 {
DispatchQueue.main.async {
let cmTimeRange = (timeRanges![0] as AnyObject).timeRangeValue as CMTimeRange
self.playerView.timeSlider.bufferValue = Float(CMTimeGetSeconds(cmTimeRange.start) + CMTimeGetSeconds(cmTimeRange.duration))
}
}

我有一个带有变量的自定义 slider ,我将其定义为 bufferValue 您可以在 slider 内覆盖 func draw(_ rect: CGRect) 并在缓冲区运行时应用更改.

我通过在 didSet 中调用 self.setNeedsDisplay() 来让 draw 随时调用 bufferValue 更改

    @IBInspectable var bufferValue : Float = 0 {
didSet {
if(bufferValue < 0 ) {
bufferValue = 0
} else if (bufferValue > maximumValue) {
bufferValue = maximumValue
}
self.setNeedsDisplay()
}
}

关于avplayer - 具有缓冲进度的 UISlider - AVPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52094802/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com