Áp dụng chế độ cài đặt cho chú thích của hệ thống

Android TV cung cấp các chế độ cài đặt cho phép người dùng thiết lập lựa chọn ưu tiên về phụ đề ở một nơi tập trung để tạo trải nghiệm nhất quán trên các ứng dụng đa phương tiện.

Các chế độ cài đặt này cho phép người dùng bật phụ đề, chọn ngôn ngữ ưu tiên và xác định kiểu phụ đề tuỳ chỉnh dựa trên nhu cầu của họ. Người dùng cũng có thể chỉ định xem họ có muốn phụ đề đơn giản ở mức độ đọc của học sinh lớp 3 hay không, còn được gọi là "Easy Reader" (Dễ đọc).

Hướng dẫn này trình bày cách lấy và áp dụng chế độ cài đặt phụ đề do hệ thống cung cấp cho phụ đề trong ứng dụng của bạn.

Tìm các lựa chọn về phụ đề (bao gồm cả bản xem trước kiểu phụ đề đã chọn) trong phần Cài đặt > Hỗ trợ tiếp cận > Phụ đề:

Trình đơn cài đặt phụ đề trên Android TV.
Hình 1. Trang cài đặt phụ đề.

Lấy CaptioningManager

Từ một hoạt động, hãy lấy dịch vụ CaptioningManager từ Context:

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

Xử lý các thay đổi về chế độ cài đặt phụ đề

Xử lý các thay đổi về chế độ cài đặt phụ đề bằng cách triển khai lớp 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();
    }

  });
}

Ngoài ra, bạn có thể gọi trực tiếp phương thức getUserStyle:

CaptionStyle systemCaptionStyle = captioningManager.getUserStyle();