您可以使用TextFormat類別為隱藏式字幕軌跡提供樣式資訊。 這會設定播放器所顯示之任何隱藏式字幕的樣式。
此類別會封裝隱藏式字幕樣式資訊,例如字型型別、大小、顏色和背景不透明度。 關聯的協助程式類別, TextFormatBuilder
,有助於使用隱藏式字幕樣式設定。
您可以使用TVSDK方法設定隱藏式字幕文字的樣式。
等候媒體播放器至少處於「已準備」狀態。
建立 TextFormatBuilder
執行個體。
您可以現在提供所有隱藏式字幕樣式引數,或稍後再設定。
TVSDK會將隱藏式字幕樣式資訊封裝在 TextFormat
介面。 此 TextFormatBuilder
類別會建立實作此介面的物件。
public TextFormatBuilder(
Font font,
Size size,
FontEdge fontEdge,
Color fontColor,
Color backgroundColor,
Color fillColor,
Color edgeColor,
int fontOpacity,
int backgroundOpacity,
int fillOpacity)
若要取得實作物件的參照 TextFormat
介面,呼叫 TextFormatBuilder.toTextFormat
公用方法。
這會傳回 TextFormat
可套用至媒體播放器的物件。
public TextFormat toTextFormat()
您可以執行下列任一項動作,取得目前的隱藏式字幕樣式設定:
透過取得所有樣式設定 MediaPlayer.getCCStyle
.
傳回值是 TextFormat
介面。
/**
* @return the current closed captioning style.
* If no style was previously set, it returns a TextFormat object
* with default values for each attribute.
* @throws IllegalStateException if media player was already released.
*/
public TextFormat getCCStyle() throws IllegalStateException;
透過,一次取得一個設定 TextFormat
介面getter方法。
public Color getFontColor();
public Color getBackgroundColor();
public Color getFillColor(); // retrieve the font fill color
public Color getEdgeColor(); // retrieve the font edge color
public Size getSize(); // retrieve the font size
public FontEdge getFontEdge(); // retrieve the font edge type
public Font getFont(); // retrieve the font type
public int getFontOpacity();
public int getBackgroundOpacity();
若要變更樣式設定,請執行下列任一項動作:
您無法變更WebVTT註解的大小。
使用setter方法 MediaPlayer.setCCStyle
,傳遞的例項 TextFormat
介面:
/**
* Sets the closed captioning style. Used to control the closed captioning font,
* size, color, edge and opacity.
*
* This method is safe to use even if the current media stream doesn't have closed
* captions.
*
* @param textFormat
* @throws IllegalStateException
*/
public void setCCStyle(TextFormat textFormat) throws IllegalStateException;
使用 TextFormatBuilder
類別,定義個別的setter方法。
此 TextFormat
介面會定義不可變物件,因此只有getter方法而沒有setter。 您只能使用設定隱藏式字幕樣式引數 TextFormatBuilder
類別:
// set font type
public void setFont(Font font)
public void setBackgroundColor(Color backgroundColor)
public void setFillColor(Color fillColor)
// set the font-edge color
public void setEdgeColor(Color edgeColor)
// set the font size
public void setSize(Size size)
// set the font edge type
public void setFontEdge(FontEdge fontEdge)
public void setFontOpacity(int fontOpacity)
public void setBackgroundOpacity(int backgroundOpacity)
// set the font-fill opacity level
public void setFillOpacity(int fillOpacity)
public void setFontColor(Color fontColor)
設定隱藏式字幕樣式為非同步操作,因此變更可能需要幾秒鐘才會顯示在畫面上。
您可以指定數個註解樣式選項,這些選項會覆寫原始註解中的樣式選項
public TextFormatBuilder(
Font font,
Size size,
FontEdge fontEdge,
Color fontColor,
Color backgroundColor,
Color fillColor,
Color edgeColor,
int fontOpacity,
int backgroundOpacity,
int fillOpacity,
String bottomInset)
在定義預設值的選項(例如DEFAULT)中,該值是指最初指定註解時的設定。
格式 | 說明 |
---|---|
字型 | 字型型別。 只能設定為以下定義的值: TextFormat.Font 分項清單,並代表(例如,有或沒有襯線)等寬。 提示:裝置上可用的實際字型可能會有所不同,並會視需要使用替代。 通常使用含襯線的等寬做為替代字元,不過此替代字元可能是系統專屬的。 |
大小 | 註解的大小。 只能設定為以下定義的值: TextFormat.Size 分項清單:
|
字型邊緣 | 用於字型邊緣的效果,例如凸出或無。 只能設定為以下定義的值: TextFormat.FontEdge 分項清單。 |
字型顏色 | 字型顏色。 只能設定為以下定義的值: TextFormat.Color 分項清單。 |
邊緣顏色 | 邊緣效果的顏色。 可設定為字型顏色可用的任何值。 |
背景顏色 | 背景字元儲存格顏色。 只能設定為可用於字型顏色的值。 |
填色顏色 | 文字所在視窗的背景色彩。 可設定為字型顏色可用的任何值。 |
字型不透明度 | 文字的不透明度。 以從0 (完全透明)到100 (完全不透明)的百分比表示。 DEFAULT_不透明度 的為100。 |
背景不透明度 | 背景字元儲存格的不透明度。 以從0 (完全透明)到100 (完全不透明)的百分比表示。 DEFAULT_不透明度 背景為100。 |
填色不透明度 | 註解視窗背景的不透明度。 以從0 (完全透明)到100 (完全不透明)的百分比表示。 DEFAULT_不透明度 填色為0。 |
您可以指定隱藏式字幕格式。
範例1:明確指定格式值
private final MediaPlayer.PlaybackEventListener
_playbackEventListener = new MediaPlayer.PlaybackEventListener() {
@Override
public void onPrepared() {
// Set CC style.
TextFormat tf = new TextFormatBuilder(TextFormat.Font.DEFAULT,
TextFormat.Size.DEFAULT,
TextFormat.FontEdge.DEFAULT,
TextFormat.Color.DEFAULT,
TextFormat.Color.DEFAULT,
TextFormat.Color.DEFAULT,
TextFormat.Color.DEFAULT,
TextFormat.DEFAULT_OPACITY,
TextFormat.DEFAULT_OPACITY,
TextFormat.DEFAULT_OPACITY).toTextFormat();
mediaPlayer.setCCStyle(tf);
...
}
}
範例2:在引數中指定格式值
/**
* Constructor using parameters to initialize a TextFormat.
*
* @param font
* The desired font.
* @param size
* The desired text size.
* @param fontEdge
* The desired font edge.
* @param fontColor
* The desired font color.
* @param backgroundColor
* The desired background color.
* @param fillColor
* The desired fill color.
* @param edgeColor
* The desired color to draw the text edges.
* @param fontOpacity
* The desired font opacity.
* @param backgroundOpacity
* The desired background opacity.
* @param fillOpacity
* The desired fill opacity.
*/
public TextFormatBuilder(
Font font, Size size, FontEdge fontEdge,
Color fontColor, Color backgroundColor,
Color fillColor, Color edgeColor,
int fontOpacity, int backgroundOpacity,
int fillOpacity);
/**
* Creates a TextFormat with the parameters supplied to this builder.
*/
public TextFormat toTextFormat();
/**
* Sets the text font.
* @param font The desired font
* @return This builder object to allow chaining calls
*/
public TextFormatBuilder setFont(Font font);
...