Today, one of my work required the app to remember the track selection, so that it can be resumed when the app is resumed. The easy problem are how to store the values. The tricky problem is how to set AVPlayer’s item tracks.
From the AVPlayer documentation it seems quite tedious to get the tracks options. So here is a helper class to save the day. (In swift3)
With the help of simple Swift’s enum, and extension, we can do something like this:
Here are simple usage,
// Simple usage
let player = AVPlayer()
// Get options
let subtitles = player.currentItem?.tracks(.subtitle)
// Get selected track displayName
let selectedSubtitle = player.currentItem?.selected(.subtitle)
// Select track with displayName
var success = player.currentItem?.select(.subtitle, subtitles.first)
Hope this save someone headache!