RxPlayer — AVPlayer with RxSwift

Dimitris Kalaitzidis
3 min readApr 30, 2019


A simple use case with AVPlayer and RxSwift.

Imagine you have a tableView with audio tracks, when you click on a row the track will either play or pause the player.

There will be only one function named playItem available for the user and all other functionality will be private and handled with RxSwift.

So, let me present my implementation…

The first lines of the RxPlayer class

As you can see, all the properties are kept private, we have two RxSwift subjects, one for controlling the state and one for the player’s currentItem.

For RxSwift subject you can read this post: https://medium.com/@dimitriskalaitzidis/rxswift-subjects-a2c9ff32a185

On the init() I’m subscribing to observe the state, currentItem and player.status

The state is an Enum with the following values: Playing, Paused, Failed, Unknown
currentItem is a RxPlayerItem struct with title, artist and URL
This is a subscription to AVPlayer’s Status property (an extension was made to make this possible)

Now on the viewController side

Let’s have a RxPlayer instance

I’m creating a RxPlayerItem with title, artist and URL and then I’m using the playItem function in order to play/pause the RxPlayer.

This will call the RxPlayer playItem function and first will make a comparison of the item’s URL with the player’s currentItem asset URL.

playItem function

If the URLs are the same the player’s state will change only from play/pause. If the URL is different, the currentItem will change.

compare function

I’m not using any play/pause logic on viewController and I don’t want that.

I just want to send a RxPlayerItem and the player must decide what to do with it.

This is not by any case a final implementation but a starting point to begin with.

You can get the code for RxPlayer.swift here: https://gist.github.com/dkalaitzidis/66c7a4b8e5a01b5738ead39f9b15203d

If you enjoyed this article you can follow me on Github (https://github.com/dkalaitzidis/) or Twitter (https://twitter.com/kalaitzidis34).



Dimitris Kalaitzidis

Engineering Director @ ORFIUM