Queueing audio files with AVQueuePlayer

Today's short post will cover queueing audio files using Swift. In order to do this we will be using AVQueuePlayer.

Import AVFoundation in order to use the AVQueuePlayer

Create an instance of AVQueuePlayer

Ps. Hold it as instance variable. If you create it as local variable, you will lose it after exiting the scope and audio won't play.

Add audio file to queue

Here we will be adding a file which is added to our project. In order to do this, we create an instance of AVPlayerItem and add it to an existing queue. Passing nil as second argument of insertItem function will insert it at the end of the queue.


Tip: Once you call a play method, you can keep adding the files to queue and they will automatically start.

This article is cross-posted with my my personal blog.