Playlist Player for iOS

DVPlaylistPlayer Title

A player is a core part of your video application. If you ever have tried to build a player into your application, you probably know that you need some time to set up and customize it. We would venture to say, that mostly you need a player to run playlists rather than individual videos. In this post, we would like to tell you about our open source component we use to play back videos in our Together project. Please welcome to our playlist player, DVPlaylistPlayer.

Yet Another Video Player?

Very often, when developing a new player from scratch you have to run through all the same steps you have followed many times before. Each time you need to install an observer to monitor playback start or end, you have to code all the same methods to control volume, etc.

As our iOS team is mainly engaged in development of video player based applications, we often find ourselves in a similar situation. And this has led us to creation of a simple generic component, a playlist player based on the standard AVPlayer.

Our goal in mind has been to provide you (and ourselves as well) with a simple, ready-to-use and flexible player template including the necessary minimum of features. It already has all components and methods needed for playback, so you don’t have to do anything manually to run videos from playlists. Should you ever need a simple video player, just take it, build it into your application, feed videos to it and, its all OK! The player has a set of standard playback features, i.e. play, stop, pause, next, previous, volume and mute/unmute.

However, if you need to customize the player or add a new feature to it, also it’s no problem. The AVPlayer that is at the core of the playlist player is part of the player’s public interface. So, should you need to enhance your player with something new, just revise this component.

Player Structure

When you create a highly customizable generic component, its core element has to be very flexible. In the iOS environment, there are but two template players that you can use as a foundation for the video player of your dream. Those are MPMoviePlayerController of the Media Player framework and AVFoundation’s AVPlayer. As you may have learned from our post titled "Video Playback in iOS Applications", where we compared the above components, MPMoviePlayerController offers no outstanding customizability. On the contrary, AVPlayer is an ideal solution to be at the heart of the player of your own design. It is of little use by itself, but when properly configured, it is very easy to use and provides a variety of options of implementation and customization.

As it has been said above, we have chosen AVPlayer as a basis for video playback. Then, to handle sound we have created the DVAudioSession class. It is based on MPMusicPlayerController and AudioSession from the AudioToolbox framework. As a separate UIView to accept the player image, we have created the DVPlaylistPlayerView class. You can include it into your view hierarchy as the player’s main window. This UIView is accessible from the main DVPlaylistPlayer class using the playerView property.

Our player differentiates from the others by our specific approach to playlist interaction. In most of cases, the playlist is an array of content units to be played back. This array is passed directly to the player. By our insight, instead of having to store an AVPlayerItem array, the player receives video using its data source method, similarly to UITableView. So, when starting playback of a new video, the player calls the queuePlayer:playerItemAtIndex: method, so you just need to correctly pass the AVPlayerItem class object to this method. As a result of such separation, the playlist can be changed and shuffled flexibly right during playback. Also, the player does not have to store an unnecessary data array, which ensures lower memory consumption. The player’s data source should implement the DVPlaylistPlayerDataSource protocol.

Playback events, such as start playback, end playback, stop playback, can be retrieved via a delegate method implementing the DVPlaylistPlayerDelegate protocol. Here is the complete list of events that you can be alerted of.

  1. Start playback.

  2. Pause.

  3. Resume playback.

  4. Stop (after the stop method is called).

  5. Stop (after the video playback is complete).

  6. Next video.

  7. Previous video.

  8. Start player buffering.

  9. Mute.

  10. Unmute.

  11. Change volume.

  12. Playback error.

To retrieve video buffering events (if the video is not stored locally), the observer is set to the playbackLikelyToKeepUp property. For this purpose, we used a library called THObserversAndBinders. Also, we have used THObserver to alert of playback paused, resumed, started and stopped.

Player Installation

Here, you have probably got bemused and are wishing to attach the player to your project. The easiest way to install the player is to use CocoaPods. If you already use CocoaPods in your project, just add:

pod ‘DVPlaylistPlayer’, :git=>’https://github.com/denivip/DVPlaylistPlayer’

to your Podfile. Then, run the command:

pod update

from the terminal. Now the player has been added to the list of components used.

If you have not yet delved into CocoaPods, then create a file named Podfile in your project’s root directory, and then add the following lines

platform :ios, ‘5.0’
pod ‘DVPlaylistPlayer’,: git => ‘https://github.com/denivip/DVPlaylistPlayer’

You may use a version higher than 5.0, depending on your project context. Still, mind that the player needs version 5.0 or higher.

Then, just run the following command in the terminal window:

pod install

The player will have been installed to your project folder. Also, this will install the THObserversAndBinders library used by the player. Now, take care to use *.Xcworkspace instead of *.Xcodeproj as your project file.

If, for any reason, you are reluctant to use CocoaPods in your project, just download the player’s git-repository. The DVPlaylistPlayer folder hosts the player’s source code. Now, you can easily attach them to the project just by copying them to XCode.

How to Use the Player

Please find below an example of using the player.

[gist id = 5966011 bump = 1]

To use the player, please create an object of the DVPlaylistPlayer class (line 1).

Then, add the player’s image from the player to your view hierarchy (line 2).

Having created instances for the player’s delegate and data source and implemented necessary methods, we can now add the delegate and data source to the player (lines 3-4).

Finally, let’s launch the player from the first video (line 6), or whatever other video you have chosen.

***

We hope that this player will help you to create video applications of your choice and become a flexible, functional and useful enhancement to your project. Let’s enhance and develop the player together: please feel free to report bugs or share your opinions on improving the player.

Link to github: https://github.com/denivip/DVPlaylistPlayer.

Leave a Reply