Usman Khan
Usman Khan

Reputation: 3953

Hide related videos at the end of youtube video using Youtube sdk android

I am working on android application where I am showing Youtube video by using youtube sdk. I want to hide suggestions which comes at the end of the video. I have implemented YouTubePlayer.PlayerStyle.MINIMAL, which hides all buttons, but still recommendations/suggestions are coming at the end of the video. I am using YouTubeAndroidPlayerApi.jar.

My code is given below, kindly guide me to fix this issue. Thanks

class YoutubeActivity : AppCompatActivity(), YouTubePlayer.OnInitializedListener {

    private var videoId: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_youtube)

        videoId = intent.getStringExtra("videoId")

        val youTubePlayerFragment = supportFragmentManager.findFragmentById(R.id.official_player_view) as YouTubePlayerSupportFragment?
        youTubePlayerFragment?.initialize(getString(R.string.youtube_apikey), this)
    }

    override fun onInitializationSuccess(provider: YouTubePlayer.Provider, youTubePlayer: YouTubePlayer, wasRestored: Boolean) {
        if (!wasRestored) {
            youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL)
            youTubePlayer.setFullscreen(true)
            youTubePlayer.loadVideo(videoId, 1)
            youTubePlayer.setShowFullscreenButton(false)
        }
    }

    override fun onInitializationFailure(provider: YouTubePlayer.Provider, youTubeInitializationResult: YouTubeInitializationResult) {
        if (youTubeInitializationResult.isUserRecoverableError) {
            youTubeInitializationResult.getErrorDialog(this, YouTube.RECOVERY_DIALOG_REQUEST).show()
        } else {
            val errorMessage = String.format(
                    "There was an error initializing the YouTubePlayer (%1\$s)",
                    youTubeInitializationResult.toString()
            )
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show()
        }
    }
}

My XML file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".components.tutorial.fragments.YoutubeActivity">

    <fragment
        android:id="@+id/official_player_view"
        android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Upvotes: 3

Views: 1329

Answers (2)

Ravi.Dudi
Ravi.Dudi

Reputation: 1364

As 770grappenmaker suggested in the answer, i tried the workaround. so in any case someone couldn't find any solution they can try this workaround.

inside your onInitializationSuccess method implement PlayerStateChangeListener and inside onVideoEnded method bring the video back at the start point and pause it.(below is my implementation in Kotlin language)

override fun onInitializationSuccess(
            p0: YouTubePlayer.Provider?,
            p1: YouTubePlayer?,
            p2: Boolean) {
            p1!!.loadVideo(videoData.videolink)
            p1.setPlayerStateChangeListener(object:YouTubePlayer.PlayerStateChangeListener{
                override fun onLoading() {}
                override fun onLoaded(p0: String?) {}
                override fun onAdStarted() {}
                override fun onVideoStarted() {}
                override fun onVideoEnded() {
                    p1.seekToMillis(0)
                    p1.pause()
                }
                override fun onError(p0: YouTubePlayer.ErrorReason?) {}

            })
        }

Upvotes: 2

770grappenmaker
770grappenmaker

Reputation: 302

What you can try is adding an YouTubePlayer. PlayerStateChangeListener, add the onVideoEnded() method, and place youtubePlayer.seekToMillis(0) I’m not sure if it is going to work, I never used this library on Android.

Upvotes: 2

Related Questions