AVPlayer需要很长时间才能开始播放

时间:2022-10-19 18:32:55

After the update to Swift 3 I´ve realised that my App takes long time to start playing an audio file from a remote server. In Swift 2.3 this didn't´t happen. I´ve been trying to debug it all day long but I couldn't´t find anything. I´ve been printing the states of the AVPlayer at each moment and I found that it changes from Loading to Playing within seconds but then it takes around 20 seconds to really start playing the song.

更新到Swift 3后,我意识到我的应用程序需要很长时间才能从远程服务器开始播放音频文件。在Swift 2.3中,这不会发生。我一直试图调试它,但我找不到任何东西。我一直在打印AVPlayer的状态,我发现它在几秒钟内从加载变为播放,但真正开始播放歌曲大约需要20秒。

I am using Jukebox by TeodorPatras

我正在使用TeodorPatras的Jukebox

2 个解决方案

#1


7  

I finally fixed it myself with the next line of code:

我终于使用下一行代码修复了它:

player?.playImmediately(atRate: 1.0)

What that line does is, it starts playing immediately without ensuring that the buffer it´s enough to not suffer interruptions. But in my case I prefer that over having to wait for several seconds.

该行的作用是,它立即开始播放而不确保缓冲区足以不受中断。但在我的情况下,我宁愿等待几秒钟。

#2


1  

I had the same issues with the devices with iOS 10 only. The solution to support iOS 10 and the old iOS versions is this:

我的iOS 10设备也遇到了同样的问题。支持iOS 10和旧iOS版本的解决方案是:

if #available(iOS 10.0, *) {
    player?.playImmediately(atRate: 1.0)
} else {
    player?.play()
}

#1


7  

I finally fixed it myself with the next line of code:

我终于使用下一行代码修复了它:

player?.playImmediately(atRate: 1.0)

What that line does is, it starts playing immediately without ensuring that the buffer it´s enough to not suffer interruptions. But in my case I prefer that over having to wait for several seconds.

该行的作用是,它立即开始播放而不确保缓冲区足以不受中断。但在我的情况下,我宁愿等待几秒钟。

#2


1  

I had the same issues with the devices with iOS 10 only. The solution to support iOS 10 and the old iOS versions is this:

我的iOS 10设备也遇到了同样的问题。支持iOS 10和旧iOS版本的解决方案是:

if #available(iOS 10.0, *) {
    player?.playImmediately(atRate: 1.0)
} else {
    player?.play()
}