Adottare le impostazioni di sistema relative ai sottotitoli

Su Android TV, gli utenti hanno la possibilità di impostare uno stile personalizzato per i sottotitoli codificati. Questa guida illustra come un'app può ottenere e applicare il modello fornito dal sistema stile dei sottotitoli codificati.

Le opzioni dei sottotitoli codificati sono disponibili in Impostazioni > Sistema > Accessibilità > Didascalia:

Impostazioni_sottotitoli_ATV

Come ottenere CaptioningManager

Da un'attività, puoi ottenere il servizio di sottotitoli dal suo Context utilizzando CaptioningManager:

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

Gestire le modifiche allo stile dei sottotitoli codificati

Puoi quindi gestire le modifiche allo stile dei sottotitoli implementando 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();
   
}

 
});

Per ottenere il sistema CaptionStyle, puoi chiamare getUserStyle() direttamente:

CaptionStyle systemCaptionStyle = captioningManager.getUserStyle();