Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AVPlayer for streaming is slow

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()

Things I've tried:

  • 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.

Please note, the issue is - player starts to play ONLY after the whole file was downloaded, while I want it to stream mp3.

like image 327
Vladyslav Zavalykhatko Avatar asked Sep 01 '25 23:09

Vladyslav Zavalykhatko


1 Answers

To play immediately you can try to set

player.automaticallyWaitsToMinimizeStalling = false
like image 154
Suen Avatar answered Sep 03 '25 14:09

Suen