androidandroid-youtube-api

How to show/hide captions in YouTubePlayer


I'm using a "YouTubePlayerFragment" in my android app. According to our requirements, I need to use "CHROMELESS" player style and use custom controllers. Is it any way to show/hide captions programmatically? Even syncing a str file would be great.

Layout

 <com.android.ui.widget.TouchEventLinearLayout
        android:id="@+id/youtubeFragmentLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <fragment
            android:id="@+id/youtubeFragment"
            android:name="com.google.android.youtube.player.YouTubePlayerFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.android.ui.widget.TouchEventLinearLayout>

Player Initialization:

@Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                        YouTubePlayer player, boolean wasRestored) {
        youTubePlayer = player;
        youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
        youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
        setPlayPause(isPlaying);
        initialProgressHandler();

        resumePoint = calculateResumePoint();
        if (!wasRestored) {
            if (isPlaying)
                youTubePlayer.loadVideo(mItem.getVideoId(), resumePoint);
            else
                youTubePlayer.cueVideo(mItem.getVideoId(), resumePoint);
        }
    }

Solution

  • For others who reach here for the same question, I ended up parsing srt file and adding a popup containing a textview to show the caption.

    The problem was syncing the text and video because the youtube player timer reports the position in a random period. you can fix it by adding another timer.