Применение системных настроек субтитров

В Android TV есть настройки, позволяющие пользователю задать параметры субтитров централизованно, чтобы они были одинаковыми во всех мультимедийных приложениях.

Эти настройки позволяют пользователям включать субтитры, выбирать предпочитаемый язык и определять собственный стиль субтитров в соответствии со своими потребностями. Пользователи также могут указать, предпочитают ли они упрощенные субтитры, соответствующие уровню чтения третьего класса, известные как "Easy Reader".

В этом руководстве рассказывается, как получить и применить системные настройки субтитров в приложении.

Чтобы посмотреть доступные варианты субтитров и их предварительный просмотр, выберите Настройки > Специальные возможности > Субтитры.

Меню настроек субтитров на Android TV.
Рисунок 1. Страница настроек субтитров.

Получение CaptioningManager

Получите сервис CaptioningManager из Context:

CaptioningManager captioningManager = (CaptioningManager) context.getSystemService(Context.CAPTIONING_SERVICE);

Как обрабатывать изменения в настройках субтитров

Чтобы обрабатывать изменения настроек субтитров, реализуйте класс CaptioningChangeListener:

if (captioningManager != null) {
  // Define a class to store the CaptionStyle details.
  CurrentCaptionStyle currentCaptionStyle = new CurrentCaptionStyle();
  // Define the listeners.
  captioningManager.addCaptioningChangeListener(new CaptioningChangeListener() {

    @Override
    public void onEnabledChanged(boolean enabled) {
      super.onEnabledChanged(enabled);
      Log.d(TAG, "onEnabledChanged");
      currentCaptionStyle.isEnabled = enabled;
    }

    @Override
    public void onLocaleChanged(@Nullable Locale locale) {
      super.onLocaleChanged(locale);
      Log.d(TAG, "onLocaleChanged");
      currentCaptionStyle.locale = locale;
      if (locale == null) {
        currentCaptionStyle.isEasyReaderEnabled = false;
      } else {
        currentCaptionStyle.isEasyReaderEnabled = locale.getVariant().contains("simple");
      }
    }

    @Override
    public void onFontScaleChanged(float fontScale) {
      super.onFontScaleChanged(fontScale);
      Log.d(TAG, "onFontScaleChanged");
      currentCaptionStyle.fontScale = fontScale;
    }

    @Override
    public void onUserStyleChanged(@NonNull CaptionStyle userStyle) {
      super.onUserStyleChanged(userStyle);
      Log.d(TAG, "onUserStyleChanged");
      currentCaptionStyle.hasBackgroundColor = userStyle.hasBackgroundColor();
      currentCaptionStyle.backgroundColor = userStyle.backgroundColor;
      currentCaptionStyle.backgroundOpacity = userStyle.backgroundColor >>> 24;
      currentCaptionStyle.hasForegroundColor = userStyle.hasForegroundColor();
      currentCaptionStyle.foregroundColor = userStyle.foregroundColor;
      currentCaptionStyle.foregroundOpacity = userStyle.foregroundColor >>> 24;
      currentCaptionStyle.hasWindowColor = userStyle.hasWindowColor();
      currentCaptionStyle.windowColor = userStyle.windowColor;
      currentCaptionStyle.windowOpacity = userStyle.windowColor >>> 24;
      currentCaptionStyle.hasEdgeColor = userStyle.hasEdgeColor();
      currentCaptionStyle.edgeColor = userStyle.edgeColor;
      currentCaptionStyle.hasEdgeType = userStyle.hasEdgeType();
      currentCaptionStyle.edgeType = userStyle.edgeType;
      currentCaptionStyle.typeFace = userStyle.getTypeface();
    }

  });
}

Вы также можете вызвать метод getUserStyle напрямую:

CaptionStyle systemCaptionStyle = captioningManager.getUserStyle();