要获取ExoPlayer2中的字幕列表并获取哪个字幕被选中,可以使用以下代码:

// 获取ExoPlayer实例
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(context);

// 设置字幕渲染器
DefaultTrackSelector trackSelector = new DefaultTrackSelector();
player.setTrackSelector(trackSelector);
trackSelector.setParameters(trackSelector.buildUponParameters().setPreferredTextLanguage('en'));

// 获取可用的字幕轨道
MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
if (mappedTrackInfo != null) {
    int rendererIndex = 2; // 字幕渲染器的索引
    TrackGroupArray trackGroups = mappedTrackInfo.getTrackGroups(rendererIndex);
    if (trackGroups.length > 0) {
        // 获取所有可用的字幕轨道
        for (int groupIndex = 0; groupIndex < trackGroups.length; groupIndex++) {
            TrackGroup trackGroup = trackGroups.get(groupIndex);
            for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
                Format format = trackGroup.getFormat(trackIndex);
                Log.d('Subtitle', format.language + ' - ' + format.id);
            }
        }

        // 获取当前选中的字幕轨道
        TrackSelectionArray trackSelections = trackSelector.getCurrentTrackSelections();
        if (trackSelections != null) {
            TrackSelection trackSelection = trackSelections.get(rendererIndex);
            if (trackSelection != null) {
                int selectedIndex = trackSelection.getSelectedIndexInTrackGroup();
                Format selectedFormat = trackGroups.get(0).getFormat(selectedIndex);
                Log.d('Subtitle', 'Selected subtitle: ' + selectedFormat.language + ' - ' + selectedFormat.id);
            }
        }
    }
}

在上面的代码中,我们首先获取ExoPlayer实例并设置字幕渲染器。然后,我们获取当前可用的字幕轨道并打印它们的语言和ID。最后,我们获取当前选中的字幕轨道并打印其语言和ID。请注意,此代码假定字幕渲染器的索引为2。如果您的应用程序中有多个渲染器,请相应地更改此值。

ExoPlayer2 字幕列表获取及选中字幕识别

原文地址: https://www.cveoy.top/t/topic/jNHA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录