From 5bdf9fb0ce6b735adf61dce8134866cf1db649ba Mon Sep 17 00:00:00 2001 From: Selvin Naz Date: Mon, 29 Mar 2021 07:52:58 -0600 Subject: [PATCH] Added a new function 'currentMediaTrack' to get the current track from the VersaPlayerItem --- .../Source/Core/Player/VersaPlayerItem.swift | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/VersaPlayer/Classes/Source/Core/Player/VersaPlayerItem.swift b/VersaPlayer/Classes/Source/Core/Player/VersaPlayerItem.swift index 02d35bb..d8e611f 100755 --- a/VersaPlayer/Classes/Source/Core/Player/VersaPlayerItem.swift +++ b/VersaPlayer/Classes/Source/Core/Player/VersaPlayerItem.swift @@ -28,6 +28,12 @@ open class VersaPlayerItem: AVPlayerItem { deinit { } + + private func convert(with mediaSelectionOption: AVMediaSelectionOption, group: AVMediaSelectionGroup) -> VersaPlayerMediaTrack { + let title = mediaSelectionOption.displayName + let language = mediaSelectionOption.extendedLanguageTag ?? "none" + return VersaPlayerMediaTrack(option: mediaSelectionOption, group: group, name: title, language: language) + } private func tracks(for characteristic: AVMediaCharacteristic) -> [VersaPlayerMediaTrack] { guard let group = asset.mediaSelectionGroup(forMediaCharacteristic: characteristic) else { @@ -35,11 +41,18 @@ open class VersaPlayerItem: AVPlayerItem { } let options = group.options let tracks = options.map { (option) -> VersaPlayerMediaTrack in - let title = option.displayName - let language = option.extendedLanguageTag ?? "none" - return VersaPlayerMediaTrack(option: option, group: group, name: title, language: language) + return convert(with: option, group: group) } return tracks } + public func currentMediaTrack(for characteristic: AVMediaCharacteristic) -> VersaPlayerMediaTrack? { + + if let tracks = asset.mediaSelectionGroup(forMediaCharacteristic: characteristic), let currentTrack = currentMediaSelection.selectedMediaOption(in: tracks) { + return convert(with: currentTrack, group: tracks) + } + + return nil + } + }