Skip to content

如何获取 AliPlayer 的 trackInfos 以及怎样切换这些 track ?

changsanjiang edited this page Dec 10, 2020 · 3 revisions
#import <SJBaseVideoPlayer/SJAliMediaPlaybackController.h>

    SJAliMediaPlaybackController *playbackController = SJAliMediaPlaybackController.new;
    _player.playbackController = playbackController

    // 获取到track后, 查询是否有其他清晰度的track
    __weak typeof(self) _self = self;
    playbackController.onTrackReadyExeBlock = ^(SJAliMediaPlaybackController * _Nonnull playbackController) {
        __strong typeof(_self) self = _self;
        if ( !self ) return;
        SJAliMediaPlayer *player = playbackController.currentPlayer;
        SJVideoPlayerURLAsset *curAsset = playbackController.media;
        AVPTrackInfo *curInfo = [player currentTrackInfo:AVPTRACK_TYPE_SAAS_VOD];
        NSArray<AVPTrackInfo *> *trackInfos = player.trackInfos;
        NSMutableArray<SJVideoPlayerURLAsset *> *definitions = [NSMutableArray arrayWithCapacity:trackInfos.count];
        // 查询其他清晰度的track
        // 创建相应的asset, 用于切换
        for ( AVPTrackInfo *info in trackInfos ) {
            if ( info.trackType == AVPTRACK_TYPE_SAAS_VOD ) {
                SJVideoPlayerURLAsset *one = nil;
                NSString *fullname = [NSString stringWithFormat:@"清晰度 %@", info.trackDefinition];
                NSString *lastname = fullname;
                
                if ( info.trackIndex != curInfo.trackIndex ) {
                    // 创建其他清晰度的asset
                    one = [SJVideoPlayerURLAsset.alloc initWithSource:curAsset.source subTrackInfo:info playModel:curAsset.playModel];
                    one.definition_fullName = fullname;
                    one.definition_lastName = lastname;
                }
                else {
                    // 当前正在播放的asset
                    curAsset.definition_fullName = fullname;
                    curAsset.definition_lastName = lastname;
                    one = curAsset;
                }
                [definitions addObject:one];
            }
        }
        
        if ( definitions.count != 0 ) {
            self.player.definitionURLAssets = definitions;
        }
    };