Android TV には、ユーザーが字幕の設定を一元的に行い、メディアアプリ全体で一貫したエクスペリエンスを実現できる設定が用意されています。
これらの設定により、ユーザーは字幕を有効にしたり、優先する言語を選択したり、ニーズに合わせてカスタムの字幕スタイルを定義したりできます。また、小学校 3 年生程度の読解レベルの簡単な字幕(「Easy Reader」)を優先するかどうかも指定できます。
このガイドでは、システム提供の字幕設定を取得してアプリの字幕に適用する方法について説明します。
選択した字幕スタイルのプレビューなど、字幕のオプションは [設定] > [ユーザー補助] > [字幕] にあります。
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();