Skip to content

Instantly share code, notes, and snippets.

@medyo
Last active June 7, 2023 13:48
Show Gist options
  • Save medyo/f226b967213c3b8ec6f6bebb5338a492 to your computer and use it in GitHub Desktop.
Save medyo/f226b967213c3b8ec6f6bebb5338a492 to your computer and use it in GitHub Desktop.
Solution: YouTubePlayerSupportFragment that supports androidx
package com.google.android.youtube.player;
/**
* Please create this directories schema com.google.android.youtube.player and post the file there
*/
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.google.android.youtube.player.internal.ab;
public class YouTubePlayerSupportFragmentX extends Fragment implements YouTubePlayer.Provider {
private final YouTubePlayerSupportFragmentX.a a = new YouTubePlayerSupportFragmentX.a();
private Bundle b;
private YouTubePlayerView c;
private String d;
private YouTubePlayer.OnInitializedListener e;
private boolean f;
public static YouTubePlayerSupportFragmentX newInstance() {
return new YouTubePlayerSupportFragmentX();
}
public YouTubePlayerSupportFragmentX() {
}
public void initialize(String var1, YouTubePlayer.OnInitializedListener var2) {
this.d = ab.a(var1, "Developer key cannot be null or empty");
this.e = var2;
this.a();
}
private void a() {
if (this.c != null && this.e != null) {
this.c.a(this.f);
this.c.a(this.getActivity(), this, this.d, this.e, this.b);
this.b = null;
this.e = null;
}
}
public void onCreate(Bundle var1) {
super.onCreate(var1);
this.b = var1 != null ? var1.getBundle("YouTubePlayerSupportFragment.KEY_PLAYER_VIEW_STATE") : null;
}
public View onCreateView(LayoutInflater var1, ViewGroup var2, Bundle var3) {
this.c = new YouTubePlayerView(this.getActivity(), (AttributeSet)null, 0, this.a);
this.a();
return this.c;
}
public void onStart() {
super.onStart();
this.c.a();
}
public void onResume() {
super.onResume();
this.c.b();
}
public void onPause() {
this.c.c();
super.onPause();
}
public void onSaveInstanceState(Bundle var1) {
super.onSaveInstanceState(var1);
Bundle var2 = this.c != null ? this.c.e() : this.b;
var1.putBundle("YouTubePlayerSupportFragment.KEY_PLAYER_VIEW_STATE", var2);
}
public void onStop() {
this.c.d();
super.onStop();
}
public void onDestroyView() {
this.c.c(this.getActivity().isFinishing());
this.c = null;
super.onDestroyView();
}
public void onDestroy() {
if (this.c != null) {
FragmentActivity var1 = this.getActivity();
this.c.b(var1 == null || var1.isFinishing());
}
super.onDestroy();
}
private final class a implements YouTubePlayerView.b {
private a() {
}
public final void a(YouTubePlayerView var1, String var2, YouTubePlayer.OnInitializedListener var3) {
YouTubePlayerSupportFragmentX.this.initialize(var2, YouTubePlayerSupportFragmentX.this.e);
}
public final void a(YouTubePlayerView var1) {
}
}
}
@Razia-Whizcamp
Copy link

Wow, adding YoutubePlayerFragmentX in same package name works on 10, 11 versions
Thank you

@alangaelrojas
Copy link

That works, are u a genius!!

@rafabaptista
Copy link

rafabaptista commented May 10, 2021

Thank you so much! Saved us here after Android 11 API update.

@jobayer
Copy link

jobayer commented May 20, 2021

@medyo in line 34, from where we are getting ab in ab.a(var1, "Developer key cannot be null or empty");. It is not defined anywhere.

@medyo
Copy link
Author

medyo commented May 20, 2021

@jobayer, it's imported from import com.google.android.youtube.player.internal.ab;

@fnzainal
Copy link

thanks @medyo ,
I don't believed that package folder would worked.
Thanks! it's worked nice

@novanto
Copy link

novanto commented Jul 6, 2021

I made a kotlin version for this and have tested it on my projects. Feel free to comment if there is any problem https://gist.github.com/novanto/5ea09bebd683a2bbdd9c4f697b6a6b28

@amitabesit
Copy link

I made a kotlin version for this and have tested it on my projects. Feel free to comment if there is any problem https://gist.github.com/novanto/5ea09bebd683a2bbdd9c4f697b6a6b28

Awesome

@jimmycw74
Copy link

Hello, first of all thank you for this awesome contribution. Recently I changed in my app the targetSdkVersion to 30 and then started to see issues. Not able to play videos using sdk 30. I returned to 29 and all works fine.

@novanto
Copy link

novanto commented Jul 21, 2021

Hello, first of all thank you for this awesome contribution. Recently I changed in my app the targetSdkVersion to 30 and then started to see issues. Not able to play videos using sdk 30. I returned to 29 and all works fine.

I think the issue is related to package visibility on android 11. Just add these lines below on your manifest - below the closing </application> tag. Read here for more info

<queries>
   <intent>
     <action android:name="com.google.android.youtube.api.service.START" />
   </intent>
</queries>

@muqeeta96
Copy link

At least give variables a meaningful name. It took me time to really understand. By the way thanks

@avelycure
Copy link

You are genius @medyo, thanks a lot!!!

@tim4dev
Copy link

tim4dev commented Dec 2, 2021

Great! Thx.

@MAkif06
Copy link

MAkif06 commented Jul 20, 2022

Ekran Görüntüsü (30)

How can I post this file to libs folder ? When I try make some changes on YouTubePlayerSupportFragment file or directory, I am getting this message : "read-only ". Any idea ?

@medyo
Copy link
Author

medyo commented Jul 20, 2022

@MAkif06 you have to create the following package schema in your src

📁com
  📁 google
    📁 android
      📁 youtube
        📁 player
          📄 YouTubePlayerSupportFragmentX.java

@MAkif06
Copy link

MAkif06 commented Jul 20, 2022

Should I transfer other files such as "YouTubeApiServiceUtil" to this schema along with this YouTubePlayerSupportFragmentX ?

@medyo
Copy link
Author

medyo commented Jul 20, 2022

@MAkif06 No, no need

@MAkif06
Copy link

MAkif06 commented Jul 20, 2022

Ekran Görüntüsü (31)

I tried your advice but now I am getting this error on xml.Do you have idea about this error ? Thanks for everythink.

@medyo
Copy link
Author

medyo commented Jul 20, 2022

@MAkif06 move the content (com.google.android... ) to the main/java folder

@MAkif06
Copy link

MAkif06 commented Jul 20, 2022

Thank you very much I will try that advice.Have nice day.

@MAkif06
Copy link

MAkif06 commented Jul 21, 2022

Thank you very much.I solved to problem thank to your advices.Have nice day.

@nvrqt03
Copy link

nvrqt03 commented Aug 11, 2022

Hi @medyo , I'm getting an error here - " Cannot access 'a': it is package-private in 'YouTubePlayerView'", as well as "Cannot access '': it is package-private in 'YouTubePlayerView'". any thoughts on how I can resolve this?

@tscholze
Copy link

I just wanna +1 @nvrqt03 question. Did you (@nvrqt03 ) fix it already? :)

@nvrqt03
Copy link

nvrqt03 commented Aug 26, 2022

@tscholze yes I did, thanks! Discussion here, but I think it was mainly due to making sure the new folder/package called com.google.android.youtube.player is in the root folder

https://johncodeos.com/how-to-embed-youtube-videos-into-your-android-app-using-kotlin/

@tscholze
Copy link

thanks buddy!

@aneesahmad28
Copy link

when I tried to move com.google.android.youtube.player... to java/main folder, it give me alert that com is already exist

@aneesahmad28
Copy link

Any Solution for Android 13, as this solution is not working on android 13

@aneesahmad28
Copy link

Any Solution for Android 13, as this solution is not working on android 13

"An error occurred while initializing the YouTube Player"

@aneesahmad28
Copy link

Any Solution for Android 13, as this solution is not working on android 13

"An error occurred while initializing the YouTube Player"

Getting above mentioned error on Android 13

@dseerapu
Copy link

dseerapu commented Mar 6, 2023

Does anybody know removing youtube branding image from the Player?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment