Search code examples
androidandroid-youtube-apiandroid-tv

Nexus Player (Android TV) YouTubeAndroidPlayerApi error: "An error occurred when initializing the YouTube player."


I'm using the YouTube android player API to play some youtube videos. My code is working fine on:

Samsung S3

Samsung Galaxy Tab III

Nexus 7 (with Android 5.1.1)

But it doesn't work on the Nexus Player. I get the error:

"An error occurred when initializing the YouTube player."

My Youtube app on the Nexus player is version 1.0.5.5

I couldn't see any indications that the youtube app is out of date nor any way to update it. If that is the problem I will instructions on how to update it.

I think my manifest is ok:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="coacb.org.examplevideoondemandapp" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".vodActivity" />
        <activity
            android:name=".youtubePlayer"
            android:label="@string/title_activity_youtube_player" >
        </activity>
        <activity
            android:name=".webviewInfo"
            android:label="@string/title_activity_webview_info" >
        </activity>
    </application>

</manifest>

Thanks in advance


Solution

  • Thanks to the suggestions and comments from dextor and ianhanniballake I was able to get it to work good with the following code:

        String videoID = "ARM42-eorzE";//youtube video id
        if (getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEVISION)
                || getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)){
            Intent youtubeIntent = YouTubeIntents.createPlayVideoIntent(getContext(), videoID);
            context.startActivity(youtubeIntent);
        } else {
            Intent myIntent = new Intent(getContext(), youtubePlayer.class);
            myIntent.putExtra("youtubeID",videoID);
            context.startActivity(myIntent);
        }