FileDocCategorySizeDatePackage
TimedText.javaAPI DocAndroid 5.1 API24333Thu Mar 12 22:22:30 GMT 2015android.media

TimedText

public final class TimedText extends Object
Class to hold the timed text's metadata, including:
  • The characters for rendering
  • The rendering position for the timed text

To render the timed text, applications need to do the following:

  • Implement the {@link MediaPlayer.OnTimedTextListener} interface
  • Register the {@link MediaPlayer.OnTimedTextListener} callback on a MediaPlayer object that is used for playback
  • When a onTimedText callback is received, do the following:
    • call {@link #getText} to get the characters for rendering
    • call {@link #getBounds} to get the text rendering area/region
see
android.media.MediaPlayer

Fields Summary
private static final int
FIRST_PUBLIC_KEY
private static final int
KEY_DISPLAY_FLAGS
private static final int
KEY_STYLE_FLAGS
private static final int
KEY_BACKGROUND_COLOR_RGBA
private static final int
KEY_HIGHLIGHT_COLOR_RGBA
private static final int
KEY_SCROLL_DELAY
private static final int
KEY_WRAP_TEXT
private static final int
KEY_START_TIME
private static final int
KEY_STRUCT_BLINKING_TEXT_LIST
private static final int
KEY_STRUCT_FONT_LIST
private static final int
KEY_STRUCT_HIGHLIGHT_LIST
private static final int
KEY_STRUCT_HYPER_TEXT_LIST
private static final int
KEY_STRUCT_KARAOKE_LIST
private static final int
KEY_STRUCT_STYLE_LIST
private static final int
KEY_STRUCT_TEXT_POS
private static final int
KEY_STRUCT_JUSTIFICATION
private static final int
KEY_STRUCT_TEXT
private static final int
LAST_PUBLIC_KEY
private static final int
FIRST_PRIVATE_KEY
private static final int
KEY_GLOBAL_SETTING
private static final int
KEY_LOCAL_SETTING
private static final int
KEY_START_CHAR
private static final int
KEY_END_CHAR
private static final int
KEY_FONT_ID
private static final int
KEY_FONT_SIZE
private static final int
KEY_TEXT_COLOR_RGBA
private static final int
LAST_PRIVATE_KEY
private static final String
TAG
private final HashMap
mKeyObjectMap
private int
mDisplayFlags
private int
mBackgroundColorRGBA
private int
mHighlightColorRGBA
private int
mScrollDelay
private int
mWrapText
private List
mBlinkingPosList
private List
mHighlightPosList
private List
mKaraokeList
private List
mFontList
private List
mStyleList
private List
mHyperTextList
private android.graphics.Rect
mTextBounds
private String
mTextChars
private Justification
mJustification
Constructors Summary
public TimedText(android.os.Parcel parcel)

param
obj the byte array which contains the timed text.
throws
IllegalArgumentExcept if parseParcel() fails. {@hide}

        if (!parseParcel(parcel)) {
            mKeyObjectMap.clear();
            throw new IllegalArgumentException("parseParcel() fails");
        }
    
Methods Summary
private booleancontainsKey(int key)

        if (isValidKey(key) && mKeyObjectMap.containsKey(key)) {
            return true;
        }
        return false;
    
public android.graphics.RectgetBounds()
Get the rectangle area or region for rendering the timed text as specified by a Rect object.

return
the rectangle region to render the characters in the timed text. If no bounds information is available (a null is returned), render the timed text at the center bottom of the display.

        return mTextBounds;
    
private java.lang.ObjectgetObject(int key)

        if (containsKey(key)) {
            return mKeyObjectMap.get(key);
        } else {
            throw new IllegalArgumentException("Invalid key: " + key);
        }
    
public java.lang.StringgetText()
Get the characters in the timed text.

return
the characters as a String object in the TimedText. Applications should stop rendering previous timed text at the current rendering region if a null is returned, until the next non-null timed text is received.

        return mTextChars;
    
private booleanisValidKey(int key)

        if (!((key >= FIRST_PUBLIC_KEY) && (key <= LAST_PUBLIC_KEY))
                && !((key >= FIRST_PRIVATE_KEY) && (key <= LAST_PRIVATE_KEY))) {
            return false;
        }
        return true;
    
private java.util.SetkeySet()

        return mKeyObjectMap.keySet();
    
private booleanparseParcel(android.os.Parcel parcel)

        parcel.setDataPosition(0);
        if (parcel.dataAvail() == 0) {
            return false;
        }

        int type = parcel.readInt();
        if (type == KEY_LOCAL_SETTING) {
            type = parcel.readInt();
            if (type != KEY_START_TIME) {
                return false;
            }
            int mStartTimeMs = parcel.readInt();
            mKeyObjectMap.put(type, mStartTimeMs);

            type = parcel.readInt();
            if (type != KEY_STRUCT_TEXT) {
                return false;
            }

            int textLen = parcel.readInt();
            byte[] text = parcel.createByteArray();
            if (text == null || text.length == 0) {
                mTextChars = null;
            } else {
                mTextChars = new String(text);
            }

        } else if (type != KEY_GLOBAL_SETTING) {
            Log.w(TAG, "Invalid timed text key found: " + type);
            return false;
        }

        while (parcel.dataAvail() > 0) {
            int key = parcel.readInt();
            if (!isValidKey(key)) {
                Log.w(TAG, "Invalid timed text key found: " + key);
                return false;
            }

            Object object = null;

            switch (key) {
                case KEY_STRUCT_STYLE_LIST: {
                    readStyle(parcel);
                    object = mStyleList;
                    break;
                }
                case KEY_STRUCT_FONT_LIST: {
                    readFont(parcel);
                    object = mFontList;
                    break;
                }
                case KEY_STRUCT_HIGHLIGHT_LIST: {
                    readHighlight(parcel);
                    object = mHighlightPosList;
                    break;
                }
                case KEY_STRUCT_KARAOKE_LIST: {
                    readKaraoke(parcel);
                    object = mKaraokeList;
                    break;
                }
                case KEY_STRUCT_HYPER_TEXT_LIST: {
                    readHyperText(parcel);
                    object = mHyperTextList;

                    break;
                }
                case KEY_STRUCT_BLINKING_TEXT_LIST: {
                    readBlinkingText(parcel);
                    object = mBlinkingPosList;

                    break;
                }
                case KEY_WRAP_TEXT: {
                    mWrapText = parcel.readInt();
                    object = mWrapText;
                    break;
                }
                case KEY_HIGHLIGHT_COLOR_RGBA: {
                    mHighlightColorRGBA = parcel.readInt();
                    object = mHighlightColorRGBA;
                    break;
                }
                case KEY_DISPLAY_FLAGS: {
                    mDisplayFlags = parcel.readInt();
                    object = mDisplayFlags;
                    break;
                }
                case KEY_STRUCT_JUSTIFICATION: {

                    int horizontal = parcel.readInt();
                    int vertical = parcel.readInt();
                    mJustification = new Justification(horizontal, vertical);

                    object = mJustification;
                    break;
                }
                case KEY_BACKGROUND_COLOR_RGBA: {
                    mBackgroundColorRGBA = parcel.readInt();
                    object = mBackgroundColorRGBA;
                    break;
                }
                case KEY_STRUCT_TEXT_POS: {
                    int top = parcel.readInt();
                    int left = parcel.readInt();
                    int bottom = parcel.readInt();
                    int right = parcel.readInt();
                    mTextBounds = new Rect(left, top, right, bottom);

                    break;
                }
                case KEY_SCROLL_DELAY: {
                    mScrollDelay = parcel.readInt();
                    object = mScrollDelay;
                    break;
                }
                default: {
                    break;
                }
            }

            if (object != null) {
                if (mKeyObjectMap.containsKey(key)) {
                    mKeyObjectMap.remove(key);
                }
                // Previous mapping will be replaced with the new object, if there was one.
                mKeyObjectMap.put(key, object);
            }
        }

        return true;
    
private voidreadBlinkingText(android.os.Parcel parcel)

        int startChar = parcel.readInt();
        int endChar = parcel.readInt();
        CharPos blinkingPos = new CharPos(startChar, endChar);

        if (mBlinkingPosList == null) {
            mBlinkingPosList = new ArrayList<CharPos>();
        }
        mBlinkingPosList.add(blinkingPos);
    
private voidreadFont(android.os.Parcel parcel)

        int entryCount = parcel.readInt();

        for (int i = 0; i < entryCount; i++) {
            int id = parcel.readInt();
            int nameLen = parcel.readInt();

            byte[] text = parcel.createByteArray();
            final String name = new String(text, 0, nameLen);

            Font font = new Font(id, name);

            if (mFontList == null) {
                mFontList = new ArrayList<Font>();
            }
            mFontList.add(font);
        }
    
private voidreadHighlight(android.os.Parcel parcel)

        int startChar = parcel.readInt();
        int endChar = parcel.readInt();
        CharPos pos = new CharPos(startChar, endChar);

        if (mHighlightPosList == null) {
            mHighlightPosList = new ArrayList<CharPos>();
        }
        mHighlightPosList.add(pos);
    
private voidreadHyperText(android.os.Parcel parcel)

        int startChar = parcel.readInt();
        int endChar = parcel.readInt();

        int len = parcel.readInt();
        byte[] url = parcel.createByteArray();
        final String urlString = new String(url, 0, len);

        len = parcel.readInt();
        byte[] alt = parcel.createByteArray();
        final String altString = new String(alt, 0, len);
        HyperText hyperText = new HyperText(startChar, endChar, urlString, altString);


        if (mHyperTextList == null) {
            mHyperTextList = new ArrayList<HyperText>();
        }
        mHyperTextList.add(hyperText);
    
private voidreadKaraoke(android.os.Parcel parcel)

        int entryCount = parcel.readInt();

        for (int i = 0; i < entryCount; i++) {
            int startTimeMs = parcel.readInt();
            int endTimeMs = parcel.readInt();
            int startChar = parcel.readInt();
            int endChar = parcel.readInt();
            Karaoke kara = new Karaoke(startTimeMs, endTimeMs,
                                       startChar, endChar);

            if (mKaraokeList == null) {
                mKaraokeList = new ArrayList<Karaoke>();
            }
            mKaraokeList.add(kara);
        }
    
private voidreadStyle(android.os.Parcel parcel)

        boolean endOfStyle = false;
        int startChar = -1;
        int endChar = -1;
        int fontId = -1;
        boolean isBold = false;
        boolean isItalic = false;
        boolean isUnderlined = false;
        int fontSize = -1;
        int colorRGBA = -1;
        while (!endOfStyle && (parcel.dataAvail() > 0)) {
            int key = parcel.readInt();
            switch (key) {
                case KEY_START_CHAR: {
                    startChar = parcel.readInt();
                    break;
                }
                case KEY_END_CHAR: {
                    endChar = parcel.readInt();
                    break;
                }
                case KEY_FONT_ID: {
                    fontId = parcel.readInt();
                    break;
                }
                case KEY_STYLE_FLAGS: {
                    int flags = parcel.readInt();
                    // In the absence of any bits set in flags, the text
                    // is plain. Otherwise, 1: bold, 2: italic, 4: underline
                    isBold = ((flags % 2) == 1);
                    isItalic = ((flags % 4) >= 2);
                    isUnderlined = ((flags / 4) == 1);
                    break;
                }
                case KEY_FONT_SIZE: {
                    fontSize = parcel.readInt();
                    break;
                }
                case KEY_TEXT_COLOR_RGBA: {
                    colorRGBA = parcel.readInt();
                    break;
                }
                default: {
                    // End of the Style parsing. Reset the data position back
                    // to the position before the last parcel.readInt() call.
                    parcel.setDataPosition(parcel.dataPosition() - 4);
                    endOfStyle = true;
                    break;
                }
            }
        }

        Style style = new Style(startChar, endChar, fontId, isBold,
                                isItalic, isUnderlined, fontSize, colorRGBA);
        if (mStyleList == null) {
            mStyleList = new ArrayList<Style>();
        }
        mStyleList.add(style);