Rohit B
Rohit B

Reputation: 165

Youtube API crash when no Video ID is present in Android Studio

I am using Youtube API in my application and it works okay, but the problem is that when I do not put any youtube video id in it, it gets crashed and my application gets closed, I do not know why this problem is occuring. I am also using Firebase and from Firebase Database I am geting the Video ID. the other problem I am getting with Youtube API is that if there is no video ID present the video player should hide itself but it is always visable, please help.

I am using Android studio

Youtube API xml file:

 <com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtuber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           />

Youtube FirebaseAdapter class:

 public void setYoutube(final String youtube){
            final YouTubePlayerView youPlay = (YouTubePlayerView) mView.findViewById(R.id.youtuber);
            youPlay.initialize("KEY",
                    new YouTubePlayer.OnInitializedListener() {
                        @Override
                        public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                                            YouTubePlayer youTubePlayer, boolean b) {

                            youTubePlayer.loadVideo(youtube);
                        }
                        @Override
                        public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                            YouTubeInitializationResult youTubeInitializationResult) {

                        }
                    });
        }

Upvotes: 1

Views: 443

Answers (2)

Android Enthusiast
Android Enthusiast

Reputation: 4960

In order to play videos, you need ID parameter to identify the video as stated in the Official Google Documentation.

Check this reference: https://developers.google.com/youtube/v3/docs/videos

Upvotes: 1

Tempus
Tempus

Reputation: 785

To keep it open, set up a short blank video on youtube.
Then have an "if (){...}" statement to put that video ID into the player if no other ID is available.

This can be your "ERROR" or "Holding" page.

Upvotes: 0

Related Questions