AVQueuePlayer Gapless Playback

I'm using AVQueuePlayer to play mp3 files that are loaded from a remote server. When I play the queue, there's a really short (milliseconds) gap between tracks. Are there any flags that need to be set or is there some other trick to fix this? When I try to airplay, the delay is worse (~1 second), like it's not buffering.


Here's roughly what I'm doing:


let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
let player: AVQueuePlayer = AVQueuePlayer()

func viewDidLoad() {
     do{
       try self.audioSession.setCategory(AVAudioSession.Category.playback, 
                    mode: AVAudioSession.Mode.default, policy: .longForm, options: [])
    } catch {
      debugPrint("error setting category", error.localizedDescription)
    }

     let url1: URL = URL(string: "https://server.com/soundfile1.mp3")!
     let url2: URL = URL(string: "https://server.com/soundfile2.mp3")!

     let track1: AVPlayerItem = AVPlayerItem(url: url1)
     let track2: AVPlayerItem = AVPlayerItem(url: url2)

     player.insert(track1, after: nil)
     player.insert(track2, after: nil)

     player.play()
}


XCode 10.1, Mojave 10.14.1, iOS 12.1

Replies

So it seems the problem is not the AVQueuePlayer, but the mp3 files. There is a small silent gap at the begining and end of some of them. I tested generated tone audio files in wav form and they played perfectly gapless. When I converted the same tone files to mp3, a small gap was added that was very similar to what I was seeing with my other mp3 files so I suspect there's something with the way mp3 files are encoded that may be causing this. The AVQueuePlayer can do gapless playback just fine as long as your files are actually gapless.