Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer takes long time to start playing

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.

I am using Jukebox by TeodorPatras

like image 692
CTABUYO Avatar asked Nov 05 '16 19:11

CTABUYO


2 Answers

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.

like image 159
CTABUYO Avatar answered Sep 18 '22 12:09

CTABUYO


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:

if #available(iOS 10.0, *) {
    player?.playImmediately(atRate: 1.0)
} else {
    player?.play()
}
like image 31
Musa almatri Avatar answered Sep 17 '22 12:09

Musa almatri