You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ExoPlayer getCurrentTrackGroups()过时,求音频格式检测代码更新方案

解决ExoPlayer getCurrentTrackGroups()过时的音频格式检测代码更新

你原有的音频格式检测代码可正常运行,但因getCurrentTrackGroups()已被标记为过时,需要更新API调用方式。直接替换为getCurrentTracks()后报错,是因为Tracks类的API与TrackGroupArray不同,不能直接用length和get()方法遍历。

原过时代码(可运行但有警告)

private static Format detectAudioFormat() {
    TrackGroupArray trackGroups = player.getCurrentTrackGroups();
    for (int i = 0; i < trackGroups.length; i++) {
        TrackGroup trackGroup = trackGroups.get(i);
        for (int j = 0; j < trackGroup.length; j++) {
            Format format = trackGroup.getFormat(j);
            if (MimeTypes.isAudio(format.sampleMimeType)) {
                return format;
            }
        }
    }
    return null;
}

错误尝试(替换后报错)

private static Format detectAudioFormat() {
    Tracks trackGroups = player.getCurrentTracks();
    for (int i = 0; i < trackGroups.length; i++) {
        TrackGroup trackGroup = trackGroups.get(i);
        for (int j = 0; j < trackGroup.length; j++) {
            Format format = trackGroup.getFormat(j);
            if (MimeTypes.isAudio(format.sampleMimeType)) {
                return format;
            }
        }
    }
    return null;
}

正确更新后的代码

根据ExoPlayer官方的替代方案,通过Tracks.getGroups()获取TrackGroupArray,沿用原有遍历逻辑即可:

private static Format detectAudioFormat() {
    Tracks tracks = player.getCurrentTracks();
    TrackGroupArray trackGroups = tracks.getGroups();
    for (int i = 0; i < trackGroups.length; i++) {
        TrackGroup trackGroup = trackGroups.get(i);
        for (int j = 0; j < trackGroup.length; j++) {
            Format format = trackGroup.getFormat(j);
            if (MimeTypes.isAudio(format.sampleMimeType)) {
                return format;
            }
        }
    }
    return null;
}

说明

  • getCurrentTrackGroups()的官方替代方式是player.getCurrentTracks().getGroups(),后者返回的TrackGroupArray与原API的类型一致,因此可以直接复用原有的遍历和格式判断逻辑。
  • 这样修改后既消除了过时警告,又保证代码功能正常运行。

内容的提问来源于stack exchange,提问作者Brenton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 19:12:27