采用系统字幕设置

在 Android TV 上,为用户提供了定义自己的字幕样式的设置。 本指南演示了应用如何获取和应用系统提供的 图片说明样式。

字幕选项位于设置 >系统 >无障碍功能 >图片说明

ATV_Caption_Settings

获取 CaptioningManager

在 activity 中,您可以使用以下代码从其 Context 获取字幕服务 CaptioningManager:

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;
    }

    @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.backgroundOpcity = 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.windowOpcity = userStyle.windowColor >>> 24;
      currentCaptionStyle.hasEdgeColor = userStyle.hasEdgeColor();
      currentCaptionStyle.edgeColor = userStyle.edgeColor;
      currentCaptionStyle.hasEdgeType = userStyle.hasEdgeType();
      currentCaptionStyle.edgeType = userStyle.edgeType;
      currentCaptionStyle.typeFace = userStyle.getTypeface();
    }

  });

如需获取系统 CaptionStyle,您可以调用 getUserStyle() 直接:

CaptionStyle systemCaptionStyle = captioningManager.getUserStyle();