codepath / android_guides

Extensive Open-Source Guides for Android Developers
guides.codepath.com
MIT License
28.32k stars 6.35k forks source link

Show/Hide subtitle in YouTubePlayer #377

Open zjamshidi opened 5 years ago

zjamshidi commented 5 years ago

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?

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);
        }
    }
wahdatkashmiri33 commented 4 years ago

`private fun initPlayer() { trackSelector = DefaultTrackSelector() exoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector) concatingMediaSource= ConcatenatingMediaSource(getMediaSource(vods)) exoPlayer.prepare(concatingMediaSource) exoPlayer.currentWindowIndex

}

override fun captions(isCaptionOff: Boolean) {

        trackSelector.parameters = ParametersBuilder().setRendererDisabled(C.TRACK_TYPE_VIDEO,isCaptionOff).build()

}`