I am embedding a Youtube video in my Android app. I have been reading several blog, posts, etc. and I am a little bit lost. My layout is
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:orientation="horizontal"
android:theme="@style/Theme.AppCompat.NoActionBar"
tools:context=".UD1">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtubePlayerView"
android:layout_width="715dp"
android:layout_height="461dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
The activity that I have created is:
public class UD1 extends YouTubeBaseActivity {
String APIkey = "aavvss";
String video1 = "hjFaqDNUVFo";
String video2 = "41cJ0mqWses";
String video3 = "uT8-YdXPNBk";
String video4 = "vSdAv931s-g";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ud1);
final YouTubePlayerView youtubePlayerView = findViewById(R.id.youtubePlayerView);
playVideo(video1, youtubePlayerView);
}
public void playVideo(final String videoId, YouTubePlayerView youTubePlayerView) {
//initialize youtube player view
youTubePlayerView.initialize(APIkey,new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo(videoId);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
}
When I run the application I get the following exception:
Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
Full stacktrace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: es.elena.learninggame, PID: 8501
java.lang.RuntimeException: Unable to start activity ComponentInfo{es.elena.learninggame/es.elena.learninggame.UD1}: android.view.InflateException: Binary XML file line #11 in es.elena.learninggame:layout/ud1: Binary XML file line #11 in es.elena.learninggame:layout/ud1: Error inflating class com.google.android.youtube.player.YouTubePlayerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Caused by: android.view.InflateException: Binary XML file line #11 in es.elena.learninggame:layout/ud1: Binary XML file line #11 in es.elena.learninggame:layout/ud1: Error inflating class com.google.android.youtube.player.YouTubePlayerView
Caused by: android.view.InflateException: Binary XML file line #11 in es.elena.learninggame:layout/ud1: Error inflating class com.google.android.youtube.player.YouTubePlayerView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:854)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:438)
at android.app.Activity.setContentView(Activity.java:3326)
at es.elena.learninggame.UD1.onCreate(UD1.java:28)
at android.app.Activity.performCreate(Activity.java:7783)
at android.app.Activity.performCreate(Activity.java:7772)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
at com.google.android.youtube.player.YouTubePlayerView.<init>(Unknown Source:19)
E/AndroidRuntime: at com.google.android.youtube.player.YouTubePlayerView.(Unknown Source:1) ... 28 more
However, my activity already extends YouTubeBaseActivity. As said, I have read several blogs, including StackOverflow and I didn't solve the error. Could please anyone guide me?
Thanks in advance
The Youtube Initialization is not happening properly in your Activity. Change your activity like this.
public class UD1 extends YouTubeBaseActivityextends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
String APIkey = "aassvv";
String video1 = "hjFaqDNUVFo";
String video2 = "41cJ0mqWses";
String video3 = "uT8-YdXPNBk";
String video4 = "vSdAv931s-g";
private static final int RECOVERY_DIALOG_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ud1);
final YouTubePlayerView youtubePlayerView = findViewById(R.id.youtubePlayerView);
// Initializing your video player like this
youtubePlayerView.initialize(APIkey, this);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
Toast.makeText(this, errorReason.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.loadVideo(video1);
// Hiding player controls
player.setPlayerStyle(PlayerStyle.CHROMELESS);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(APIkey, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtubePlayerView);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With