accessibilityexoplayervideo-subtitles

Wrong rendering (overlapping) for Android Captions/Subtitles at very large


In Android System settings --> Accessibility --> Subtitle Settings

enter image description here

is set to very large. Content with subtitles on my Exoplayer is not rendered properly - it is overlapping when watching in portrait mode! For landscape mode it is looking nice.

(See the following attached examples for the overlapping subtitles in portrait mode)

enter image description here

How can I fix this? What am I doing wrong?


Solution

  • ExoPlayer ignores Android’s system caption settings, so you must configure subtitle styling and layout manually:

    Layout: use StyledPlayerView

    <!-- res/layout/activity_player.xml -->
    <com.google.android.exoplayer2.ui.StyledPlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:use_controller="true"
        app:subtitleStyle="@style/CustomSubtitleStyle"
        app:subtitleBottomPadding="16dp" />
    

    Define subtitle style

    <!-- res/values/styles.xml -->
    <style name="CustomSubtitleStyle" parent="TextAppearance.AppCompat.Caption">
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:background">@android:color/black</item>
        <item name="android:padding">4dp</item>
    </style>
    

    Limit number of lines

    StyledPlayerView playerView = findViewById(R.id.player_view);
    SubtitleView subtitleView = playerView.getSubtitleView();
    subtitleView.setMaxLines(2);  // or 1
    

    Apply system caption settings

    CaptioningManager cm = (CaptioningManager) getSystemService(Context.CAPTIONING_SERVICE);
    float scale = cm.getFontScale();
    subtitleView.setFractionalTextSize(SubtitleView.DEFAULT_TEXT_SIZE_FRACTION * scale);
    subtitleView.setStyle(SubtitleView.getUserDefaultStyleV19(this));