Post not yet marked as solved
Post marked as unsolved with 3 replies, 882 views
Hi, i am developing a media playback app and would like to set my own MediaSelectionCriteria for subtitles to the AVPlayer. However, this does not seem to work properly. Sometimes, the player displays the correct subtitle track and sometimes it displays no subtitles at all. The media file i use consists of a video track, a german audio track and an english subtitle track. My implementation is currently as follows:let playerItem = AVPlayerItem(asset: AVAsset(url: url))
self.player = AVPlayer(playerItem: playerItem)
self.playerViewController!.player = player
let locales = ["de-DE", "de-AT", "de", "en-US", "en-GB", "en"]
let mediaSelectionCriteria = AVPlayerMediaSelectionCriteria(preferredLanguages: locales, preferredMediaCharacteristics: nil)
self.player!.setMediaSelectionCriteria(mediaSelectionCriteria, forMediaCharacteristic: .legible)
self.player!.play()
playerItem.asset.loadValuesAsynchronously(forKeys: ["availableMediaCharacteristicsWithMediaSelectionOptions"]) {
for characteristic in playerItem.asset.availableMediaCharacteristicsWithMediaSelectionOptions {
if [.legible].contains(characteristic), let group = playerItem.asset.mediaSelectionGroup(forMediaCharacteristic: characteristic) {
DispatchQueue.main.sync {
playerItem.selectMediaOptionAutomatically(in: group)
}
}
}
}