รับการตั้งค่าคำบรรยายของระบบ

Android TV มีการตั้งค่าที่ช่วยให้ผู้ใช้ตั้งค่ากำหนดคำบรรยายแทนเสียงได้จากส่วนกลาง เพื่อสร้างประสบการณ์การใช้งานที่สอดคล้องกันในแอปสื่อ

การตั้งค่าเหล่านี้ช่วยให้ผู้ใช้เปิดใช้คำบรรยายแทนเสียง เลือกภาษาที่ต้องการ และ กำหนดรูปแบบคำบรรยายแทนเสียงที่กำหนดเองตามความต้องการได้ นอกจากนี้ ผู้ใช้ยังระบุได้ด้วยว่าต้องการคำบรรยายแทนเสียงแบบง่ายที่ระดับการอ่านประมาณชั้นประถมศึกษาปีที่ 3 หรือไม่ ซึ่งเรียกว่า "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();