diff --git a/SGPlayer/Classes/Core/SGAsset/SGTrack.h b/SGPlayer/Classes/Core/SGAsset/SGTrack.h index 9bf6117f..e7bd03ef 100644 --- a/SGPlayer/Classes/Core/SGAsset/SGTrack.h +++ b/SGPlayer/Classes/Core/SGAsset/SGTrack.h @@ -9,6 +9,26 @@ #import #import "SGDefines.h" +@class SGTrack; + +/*! + @abstract + Get track with index. +*/ +SGTrack *SGTrackWithIndex(NSArray *tracks, NSInteger index); + +/*! + @abstract + Get track with media type. +*/ +SGTrack *SGTrackWithType(NSArray *tracks, SGMediaType type); + +/*! + @abstract + Get tracks with media types. +*/ +NSArray *SGTracksWithType(NSArray *tracks, SGMediaType type); + @interface SGTrack : NSObject + (instancetype)new NS_UNAVAILABLE; diff --git a/SGPlayer/Classes/Core/SGAsset/SGTrack.m b/SGPlayer/Classes/Core/SGAsset/SGTrack.m index 0797ba60..9d3b29fd 100644 --- a/SGPlayer/Classes/Core/SGAsset/SGTrack.m +++ b/SGPlayer/Classes/Core/SGAsset/SGTrack.m @@ -9,6 +9,37 @@ #import "SGTrack.h" #import "SGTrack+Internal.h" +SGTrack *SGTrackWithIndex(NSArray *tracks, NSInteger index) +{ + for (SGTrack *obj in tracks) { + if (obj.index == index) { + return obj; + } + } + return nil; +} + +SGTrack *SGTrackWithType(NSArray *tracks, SGMediaType type) +{ + for (SGTrack *obj in tracks) { + if (obj.type == type) { + return obj; + } + } + return nil; +} + +NSArray *SGTracksWithType(NSArray *tracks, SGMediaType type) +{ + NSMutableArray *array = [NSMutableArray array]; + for (SGTrack *obj in tracks) { + if (obj.type == type) { + [array addObject:obj]; + } + } + return array.count ? [array copy] : nil; +} + @implementation SGTrack - (id)copyWithZone:(NSZone *)zone