在 ExoPlayer2 中,可以通过 TrackSelectionTrackSelector 接口来获取视频中的音轨并设置音轨。

获取音轨:

// 获取音轨选择器
MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
if (mappedTrackInfo != null) {
    // 获取音频轨道索引
    int audioRendererIndex = 0;
    for (int i = 0; i < player.getRendererCount(); i++) {
        if (player.getRendererType(i) == C.TRACK_TYPE_AUDIO) {
            audioRendererIndex = i;
            break;
        }
    }
    // 获取音频轨道选择器
    TrackSelection.Factory audioTrackSelectionFactory = new AdaptiveTrackSelection.Factory();
    TrackSelector audioTrackSelector = new DefaultTrackSelector(audioTrackSelectionFactory);
    audioTrackSelector.init(trackSelectorParameters, mappedTrackInfo);
    // 获取音频轨道信息
    TrackGroupArray audioTrackGroups = mappedTrackInfo.getTrackGroups(audioRendererIndex);
    for (int i = 0; i < audioTrackGroups.length; i++) {
        TrackGroup audioTrackGroup = audioTrackGroups.get(i);
        for (int j = 0; j < audioTrackGroup.length; j++) {
            Format audioFormat = audioTrackGroup.getFormat(j);
            // 获取音频轨道的语言
            String audioLanguage = audioFormat.language;
            // 获取音频轨道的 ID
            int audioTrackId = audioFormat.id;
            // 获取音频轨道的编码格式
            String audioCodec = audioFormat.sampleMimeType;
            // 获取音频轨道的比特率
            int audioBitrate = audioFormat.bitrate;
            // 获取音频轨道的通道数
            int audioChannelCount = audioFormat.channelCount;
            // 获取音频轨道的采样率
            int audioSampleRate = audioFormat.sampleRate;
            // 获取音频轨道的标签
            String audioLabel = audioFormat.label;
            // 获取音频轨道的描述
            String audioDescription = audioFormat.description;
            // ...
        }
    }
}

设置音轨:

// 获取音频轨道索引
int audioRendererIndex = 0;
for (int i = 0; i < player.getRendererCount(); i++) {
    if (player.getRendererType(i) == C.TRACK_TYPE_AUDIO) {
        audioRendererIndex = i;
        break;
    }
}
// 获取音频轨道选择器
TrackSelection.Factory audioTrackSelectionFactory = new AdaptiveTrackSelection.Factory();
TrackSelector audioTrackSelector = new DefaultTrackSelector(audioTrackSelectionFactory);
audioTrackSelector.init(trackSelectorParameters, mappedTrackInfo);
// 获取音频轨道信息
TrackGroupArray audioTrackGroups = mappedTrackInfo.getTrackGroups(audioRendererIndex);
// 获取音频轨道选择器中的选择索引
int audioTrackIndex = audioTrackSelector.getTrackSelections().get(0).getSelectedIndex();
// 设置音频轨道
player.setParameters(player.buildUponParameters().setSelectionOverride(audioRendererIndex, audioTrackGroups, audioTrackIndex));
exoplayer2 获取视频中的音轨并设置音轨

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

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