【问题标题】:Is there an ExoPlayer + Leanback library example for using captions?是否有使用字幕的 ExoPlayer + Leanback 库示例?
【发布时间】:2020-03-26 14:51:12
【问题描述】:

我找到了一些适用于 LeanbackExoPlayer 的示例,我可以使用所有这些示例,但我无法使用字幕/字幕。我能找到的最新 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 的内容,并在我的Fragment 中使用了com.google.android.exoplayer2.ui.PlayerView 而不是VideoFragment,并且字幕工作没有进行任何其他更改。所以就像我只需要以某种方式在Leanback 端启用它们。

【问题讨论】:

    标签: android exoplayer android-tv exoplayer2.x amazon-fire-tv


    【解决方案1】:

    要使字幕正常工作,您需要覆盖 lb_playback_fragment.xml 并添加一个 SubtitleView。

    app/src/main/res/layout/lb_playback_fragment.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <!--
        Copied from android source so that we can add the subtitle view
    -->
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/playback_fragment_root"
        android:layout_width="match_parent"
        android:transitionGroup="false"
        android:layout_height="match_parent">
    
        <androidx.leanback.widget.NonOverlappingFrameLayout
            android:id="@+id/playback_fragment_background"
            android:transitionGroup="false"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <androidx.leanback.widget.NonOverlappingFrameLayout
            android:id="@+id/playback_controls_dock"
            android:transitionGroup="true"
            android:layout_height="match_parent"
            android:layout_width="match_parent"/>
    
        <com.google.android.exoplayer2.ui.AspectRatioFrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center">
    
            <com.google.android.exoplayer2.ui.SubtitleView
                android:id="@+id/leanback_subtitles"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </com.google.android.exoplayer2.ui.AspectRatioFrameLayout>
    
    </FrameLayout>
    

    完成此操作后,您可以设置 DefaultTrackSelector 来选择所需的文本轨道。

    您可以在https://github.com/bennettpeter/android-MythTV-Leanfront 中查看基于示例leanback 播放器的完整代码。加字幕的改动在this commit

    【讨论】:

    • 我不能这样做...我收到错误无法解析 androidx.leanback.widget.NonOverlappingFrameLayout 因为它被标记为 @RestrictTo(LIBRARY_GROUP) 并且只能从同一个库组中调用.. .
    • 我已经用了几年了,还没有看到这个问题。该错误是在编译时还是运行时发生的?也许您使用的版本与我不同?
    • 这是在编译时发生的。我正在使用最新的稳定版leanback 1.0.0...我知道有 1.1rc 和 1.2alpha,但它们不是官方版本,所以我的公司不使用它们。还有 exoplayer 2.14.0。
    • 我已经设法以编程方式将 SubtitleView 插入到这个视图中,而不需要覆盖 lb_playback_fragment.xml。我通过创建自己的新派生类 PlaybackSupportFragment 来做到这一点,并创建了新的 SubtitleView(requireContext());将此添加到 root.addView(mSubtitleView, 0);最后不得不在 exo 初始化后添加这一行“ val textComponent = exoplayer?.textComponent textComponent?.addTextOutput(subtitleView);”
    • 如果您的错误是针对 java 代码报告的,您可以通过在收到错误的函数前面添加 @SuppressLint("RestrictedApi") 来防止错误。
    【解决方案2】:

    这是另一种方法——renderIndex = 0 用于视频,=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
            }
    
        }
    }
    

    【讨论】:

    • 我的问题不在于设置字幕时,如果我把Leanback 拿出来,那么字幕就可以正常工作。我认为问题是告诉Leanback 库显示字幕。
    猜你喜欢
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多