Android YouTube API "An error occurred while initializing YouTube player"
Asked Answered
I

5

20

I have an android app, which allows to play youtube video. I am using the latest YouTube API (1.2.1). I try it on a several devices within android version 4.0.4, 4.3, 4.4.4 or 5.0 and it works perfect (on every device is YouTube app version 10.-. But on one device, where is android 4.0.4 and YouTube app version 4.4.11 it does not work and get an error "An error occurred while initializing YouTube player". In documentation is written, that the minimal required version of YouTube app is 4.2.16. So I don't know, where is the problem.

Does somebody have an idea, what is wrong or how can I fix it?

Thanks a lot...

Iconium answered 18/3, 2015 at 22:25 Comment(2)
Is that still working on other version except 4.0.4 ?. I got the same error and it is not working in any device anymore. It was working fine just recently in any device.Doolie
After actualization of YouTube app, it has started working fine. Then the phone was reset and the version of YouTube app was 4.2.16 again, but everything worked fine. It is curious.Iconium
S
52

This issues coming in Android 11 and above version, due to Package visibility filtering

The limited app visibility affects the return results of methods that give information about other apps, such as queryIntentActivities(), getPackageInfo(), and getInstalledApplications(). The limited visibility also affects explicit interactions with other apps, such as starting another app's service.

For more info : https://developer.android.com/training/package-visibility

Add the below lines of code into your AndroidManifest.xml file

<queries>
   <intent>
      <action android:name="com.google.android.youtube.api.service.START" />
   </intent>
</queries>
Shere answered 23/2, 2021 at 9:35 Comment(6)
Please elaborate your answer.Maziar
for my app only the devices with android 11, I am facing the same issue but after adding these lines of code into the android manifest file it's work for me.Shere
@Bilal This should be the accepted answerHousebreaking
Add these lines of code just above <application> tag in manifest.xml. Though I still don't know why it didn't work without these lines and vice versa. I followed a tutorial and got error while the tutorial guy had no issue playing a video.Therapist
@Bilal youtube apps can disable from samsung phones.. that case same error shown, what will do that caseStlouis
Thank you, it worked for me now youtube player is working. By the way, I noticed that the queries tag should be above the application tag.Term
H
7

Update your Android youtube app to the latest version and it will work for Sure!!

Hanus answered 31/5, 2016 at 18:38 Comment(0)
C
4

I would never have thought to look in that location for the settings. It worked just as you said.

Here are the instructions for that less knowledge of the settings.

First, you need to go Settings -> battery -> app launch -> YouTube.

Now make disable the option manage automatically.

After disabling the above option, a popup will be shown.

Now make enable the option Secondary Launch (can be launched by other apps)

Candlelight answered 31/5, 2019 at 12:40 Comment(0)
T
0

If someone still getting this error then,

The problem is in the layout file, may be you defined the fixed height and width :

<com.google.android.youtube.player.YouTubePlayerView
    android:layout_width="123dp"
    android:layout_height="123dp" />

Replace height and width with "wrap_content" :

<com.google.android.youtube.player.YouTubePlayerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
Turpitude answered 12/1, 2022 at 18:50 Comment(1)
doesn't work for mePerception
C
-3

I replaced targetSdkVersion =30 to targetSdkVersion =26 and it's worked for me

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30`enter code here`
    defaultConfig {
        applicationId "com.loopwiki.youtubeplayerexample"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation files('libs/YouTubeAndroidPlayerApi.jar')
}
Cerebroside answered 18/6, 2021 at 19:32 Comment(1)
That is just a terrible advice.Maintain

© 2022 - 2024 — McMap. All rights reserved.