[我发现了一些适用于Leanback
和ExoPlayer
的示例,并且可以正常使用,但无法获得字幕/字幕。我可以找到的最新Google示例(https://github.com/android/tv-samples)在Java示例中有一个字幕按钮,但它们从未显示。 Kotlin示例的注释为// TODO(owahltinez): handle captions
。
我已经尝试对其中一个示例进行了这些更改,但没有帮助:
private void prepareMediaForPlaying(Uri mediaSourceUri) {
String userAgent = Util.getUserAgent(getActivity(), "VideoPlayerGlue");
DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(getActivity(), userAgent);
MediaSource mediaSource =
new ExtractorMediaSource(
mediaSourceUri,
defaultDataSourceFactory,
new DefaultExtractorsFactory(),
null,
null);
String subtitle = "https://subtitledomain/sintel-en.vtt";
Uri uriSubtitle = Uri.parse(subtitle);
MediaSource subtitleMediaSource = new SingleSampleMediaSource.Factory(defaultDataSourceFactory)
.createMediaSource(uriSubtitle, Format.createTextSampleFormat(null, MimeTypes.TEXT_VTT, C.SELECTION_FLAG_FORCED, "n/a"), C.TIME_UNSET);
mediaSource = new MergingMediaSource(mediaSource, subtitleMediaSource);
mPlayer.prepare(mediaSource);
}
还有此更改:
mTrackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
DefaultTrackSelector.Parameters parameters = mTrackSelector.getParameters();
mTrackSelector.setParameters(parameters.withSelectUndeterminedTextLanguage(true));
我尝试将字幕上的语言更改为EN
,但这没有帮助。我觉得我可能只是想念一些小东西,但我不知道那会是什么。
谢谢。
编辑:我创建了一个分支,删除了所有Leanback
代码,仅保留了ExoPlayer
内容,并在我的com.google.android.exoplayer2.ui.PlayerView
中使用了Fragment
而不是VideoFragment
,并且在不进行任何其他更改的情况下,字幕可以正常工作。因此,就像我只需要以某种方式在Leanback
端启用它们。
这是另一种方法-视频的renderIndex = 0,音频的renderIndex = 1,字幕/文本的= 2。
TrackGroupArray trackGroups = mappedTrackInfo.getTrackGroups(rendererIndex);
TrackSelectionArray currentTrackGroups = player.getCurrentTrackSelections();
TrackSelection currentTrackSelection = currentTrackGroups.get(rendererIndex);
for (int groupIndex = 0; groupIndex < trackGroups.length; groupIndex++) {
TrackGroup group = trackGroups.get(groupIndex);
for (int trackIndex = 0; trackIndex < group.length; trackIndex++) {
Format trackFormat = group.getFormat(trackIndex);
if(currentTrackSelection!=null &&
currentTrackSelection.getSelectedFormat()==trackFormat){
//THIS ONE IS SELECTED
}
}
}