Skip to content

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

changsanjiang edited this page Jan 8, 2021 · 3 revisions

这个是针对 AliPlayer 的 trackInfos 的切换方式, 通过 URL 切换请直接前往 切换清晰度 查看.

#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];
            }
        }
        
        self.player.definitionURLAssets = definitions;
    };