- iOS 11.0+
- Xcode 10.0+
This sample provides a catalog of HTTP Live Streams (HLS) that you can play by tapping the row in the table corresponding to the stream. To manage the download of an HLS stream, tap the accessory button associated with the stream in the row in the table. Tapping the accessory button causes a transition to a new view controller which provides an interface to initiate a download, cancel an already running download, or delete a downloaded HLS stream from the device.
The sample creates and initializes an
AVAggregate for the download of an HLS stream. Only the default media selections for each of the asset’s media selection groups are downloaded (these are indicated in the HLS playlist
EXT-X-MEDIA tags by a
DEFAULT attribute of
This sample does not support saving FairPlay Streaming (FPS) content. For a version of the sample that demonstrates how to download FPS content, see FairPlay Streaming Server SDK.
Play an HTTP Live Stream
Asset is the main user interface of this sample. It provides a list of the assets the sample can play, download, cancel download, and delete.
Asset provides a list of assets to present in the
Asset is responsible for playing downloaded assets, and it uses key-value observing (KVO) to monitor playback-related changes to the
AVPlayer objects it manages. A player item’s
status emits a KVO change notification when its status changes. The app monitors these changes and initiates playback when the
status property indicates the player item is ready to play. The app observes the
is property to determine whether an AVPlayer can play the contents of the asset in a manner that meets user expectations. The app also observes the player
current property to access the player item created for a given stream.
Stream class manages loading and reading the contents of the
Streams file in the app bundle.
To play an item, tap one of the rows in the table. Tapping the item causes a transition to a new view controller. As part of that transition, the table view creates an
Asset and assigns the appropriate asset to it, as shown in the following example:
Assigning an asset to the
Asset causes it to create an
AVPlayer for the asset, removing any previous asset in the process:
Asset uses KVO to monitor the
status, and initiates playback when the
status becomes ready to play:
Download an HTTP Live Stream
Asset is the main class in this sample that demonstrates how to manage downloading HLS streams. It includes methods for starting and canceling downloads, deleting existing assets from the user’s device, and monitoring the download.
When the user initiates a download by tapping the accessory button in the corresponding stream’s table view cell, an instance of
Asset calls the following function to create an
AVAggregate object to download multiple
AVMedia for the
AVURLAsset of the HLS stream:
You cannot save a live HLS stream while it is in progress. If you try to save a live HLS stream, the system throws an exception. Only Video On Demand (VOD) streams support offline playback.
Cancel an HTTP Live Stream
Tap the accessory button in the corresponding stream’s table view cell to reveal the accessory view, then tap Cancel to stop downloading the stream. The following function in
Asset cancels the download by calling the
Remove an HTTP Live Stream from Disk
Tap the accessory button in the corresponding stream’s table view cell to reveal the accessory view, then tap Delete to delete the downloaded stream file. The following function in
Asset removes a downloaded stream on the device. First the asset URL corresponding to the file on the device is identified, then the
remove method is called to remove the downloaded stream at the specified URL.
Measure Playback Performance
Perf contains utility code to measure key performance indicators (KPI) during streaming playback. This code makes use of the
AVPlayer for many of its calculations. An
AVPlayer object accumulates key metrics about network playback and presents them as a collection of
AVPlayer instances. Each event instance collates the data that relates to each uninterrupted period of playback.
You can view the various performance indicators in the console during playback.
For example, here’s the code to calculate the total time spent playing the stream, obtained from the