本文介绍了使用AVPlayer进行流式传输速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用AVPlayer从互联网上流式传输mp3文件,它的运行速度确实很慢.使用分析器发现,它首先下载整个文件,然后开始播放.有什么解决方法吗?
I use AVPlayer for streaming mp3 file from the internet and it works really slow. Using profiler I found out, that it downloads entire file at first, and then starts playing. Is there any workaround for this?
现在,我正在使用此代码
Right now, I'm using this code
if let player = player {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
let item = AVPlayerItem(url: url)
player.replaceCurrentItem(with: item)
} else {
player = AVPlayer(url: url)
}
player?.play()
我尝试过的事情:
- 将播放器?.play()移动到观察者,该观察者附加到该项目的 status 属性
- 使用属性 preferredForwardBufferDuration 和 preferredPeakBitRate
- move player?.play() to an observer, attached to status property of the item
- play around with properties preferredForwardBufferDuration and preferredPeakBitRate
一直以来,结果都是下载整个音频文件,然后才开始播放.
All the time the result is downloading a whole audio file and only then start of playing.
请注意,问题是-播放器仅在下载整个文件后才开始播放,而我希望它流mp3.
Please note, the issue is - player starts to play ONLY after the whole file was downloaded, while I want it to stream mp3.
推荐答案
要立即播放,您可以尝试设置
To play immediately you can try to set
player.automaticallyWaitsToMinimizeStalling = false
这篇关于使用AVPlayer进行流式传输速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!