Android TV では、ユーザーが独自の字幕ス���イルを定義できる設定が用意されています。 このガイドでは、アプリがシステムが提供する字幕スタイルを取得して適用する方法について説明します。
字幕のオプションは [設定] > [システム] > [ユーザー補助] > [字幕] にあります。
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;
}
@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();