在我的视频流应用程序中,我想实现一项功能,用户可以选择该功能以供视频使用。因此,我已经使用TrackSelectionView实现了这一点,
public void showQualitySettingsDialog(Activity activity) {
overrideVideoTrackGroup(trackSelector);
MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
if (mappedTrackInfo != null) {
int rendererIndex = getVideoRendererIndex(trackSelector);
Pair<AlertDialog, TrackSelectionView> dialogPair =
TrackSelectionView.getDialog(activity, activity.getString(R.string.video_quality), trackSelector, rendererIndex);
dialogPair.first.show();
}
}
private int getVideoRendererIndex(DefaultTrackSelector trackSelector) {
int videoRendererIndex = 0;
MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
if (mappedTrackInfo != null) {
for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) {
TrackGroupArray trackGroups = mappedTrackInfo.getTrackGroups(i);
if (trackGroups.length != 0 && player.getRendererType(i) == C.TRACK_TYPE_VIDEO) {
videoRendererIndex = i;
}
}
}
return videoRendererIndex;
}
这项工作相当不错。但是问题是,我的HLS主文件中有一些重复的视频轨道(请看测试内容)。所以我写了一种方法来对视频的TrackGroup进行排序 像这样
private TrackGroup getSortedVideoTrackGroup(@NonNull ArrayList<Format> formatList) {
Format [] formats = new Format[formatList.size()];
for (int i = 0; i < formatList.size(); i++) {
formats[i] = formatList.get(i);
}
return new TrackGroup(formats);
}
private ArrayList<Format> getSortedFormatArrayList(DefaultTrackSelector trackSelector) {
ArrayList<Format> formatArrayList = new ArrayList<>();
Format tempFormat = null;
MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
if (mappedTrackInfo != null) {
for (int trackGroups = 0; trackGroups < mappedTrackInfo.getRendererCount(); trackGroups++) {
TrackGroupArray trackGroupArray = mappedTrackInfo.getTrackGroups(trackGroups);
if (trackGroupArray.length != 0 && player.getRendererType(trackGroups) == C.TRACK_TYPE_VIDEO) {
//Trackgroup Array für videos auslesen
for (int j = 0; j < trackGroupArray.length; j++) {
TrackGroup trackGroup = trackGroupArray.get(j);
for (int k = 0; k < trackGroup.length; k++) {
Format format = trackGroup.getFormat(k);
if (tempFormat == null) {
tempFormat = format;
} else {
if (tempFormat.bitrate != format.bitrate) {
formatArrayList.add(format);
tempFormat = format;
}
}
}
}
}
}
}
return formatArrayList;
}
到目前为止,一切都很好。现在我的问题是,如何用“已排序”的TrackGroup覆盖TrackGroupArray中的视频TrackGroup,以防止显示两倍的视频轨道。
您可以使用exoplayer演示应用程序重现我的问题。