Created
January 17, 2017 14:53
-
-
Save peatiscoding/4f01a64797289fd40793837fb727c9c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AVFoundation | |
extension AVPlayerItem { | |
enum TrackType { | |
case subtitle | |
case audio | |
/** | |
Return valid AVMediaSelectionGroup is item is available. | |
*/ | |
fileprivate func characteristic(item:AVPlayerItem) -> AVMediaSelectionGroup? { | |
let str = self == .subtitle ? AVMediaCharacteristicLegible : AVMediaCharacteristicAudible | |
if item.asset.availableMediaCharacteristicsWithMediaSelectionOptions.contains(str) { | |
return item.asset.mediaSelectionGroup(forMediaCharacteristic: str) | |
} | |
return nil | |
} | |
} | |
func tracks(type:TrackType) -> [String] { | |
if let characteristic = type.characteristic(item: self) { | |
return characteristic.options.map { $0.displayName } | |
} | |
return [String]() | |
} | |
func selected(type:TrackType) -> String? { | |
guard let group = type.characteristic(item: self) else { | |
return nil | |
} | |
let selected = self.selectedMediaOption(in: group) | |
return selected?.displayName | |
} | |
func select(type:TrackType, name:String) -> Bool { | |
guard let group = type.characteristic(item: self) else { | |
return false | |
} | |
guard let matched = group.options.filter({ $0.displayName == name }).first else { | |
return false | |
} | |
self.select(matched, in: group) | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi , Just need one help.I am using this class very helpful.But if i am changing the Subtitle and after that audio same time one thing works other dont.Cannot understand why