mike
mike

Reputation: 561

Error creating YouTubePlayerView

I'm having no luck getting the youtube-api working on android. I'm trying to get the minimum working - just play a video via public access (no oauth). I have a key from google, and I installed the youtube app on my emulator. I'm fairly confident this is correct because I created a fresh project from the youtube api sample code, and it works perfectly.

When run from my project though I get:

E/YouTubeAndroidPlayerAPI: Error creating YouTubePlayerView
       com.google.android.youtube.player.internal.w$a: Exception thrown by invoked constructor in com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer
...
        Caused by: java.lang.reflect.InvocationTargetException
           at java.lang.reflect.Constructor.newInstance0(Native Method)
...

        Caused by: java.lang.IllegalArgumentException: The concrete class implementing IObjectWrapper must have exactly *one* declared private field for the wrapped object.  Preferably, this is an instance of the ObjectWrapper<T> class.
           at zst.a(SourceFile:76)

My layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:textAppearance="@android:style/TextAppearance.Small"
    android:gravity="center"
    android:text="Youtube!"/>

<fragment
    android:name="com.google.android.youtube.player.YouTubePlayerFragment"
    android:id="@+id/youtube_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

And my activity:

public class EducationVideoActivity extends YouTubeFailureRecoveryActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_education_youtube);

        YouTubePlayerFragment youTubePlayerFragment =
                (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
        youTubePlayerFragment.initialize(YoutubeKey.DEVELOPER_KEY, this);
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
                                        boolean wasRestored) {
        if (!wasRestored) {
            player.cueVideo("nCgQDjiotG0");
        }
    }

    @Override
    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
    }

}

I've tried several other examples online, and they all give the same errors. Any and all suggestions are welcome!

Upvotes: 1

Views: 735

Answers (2)

I had the some problem on my Honor 8.

First time I imported YouTubeAndroidPlayerApi.jar this way -> Right click on app folder in project -> New -> Module -> Import .JAR/.AAR Package -> Choose YouTubeAndroidPlayerApi.jar -> Finish. Then open project structure and in Dependencies tab added Module dependency. After this gradle completed and in build.gradle was compile project(':YouTubeAndroidPlayerApi'). In this way an Error creating YouTubePlayerView occurred.

So I reverted everything and imported lib like this -> copy YouTubeAndroidPlayerApi.jar into /app/libs folder -> open Project structure and instead of Module dependency I chose Jar dependency and from libs folder selected YouTubeAndroidPlayerApi.jar. After that gradle completed and now in build.gradle is compile files('libs/YouTubeAndroidPlayerApi.jar')and everything works.

As mike already mentioned in gradle should be compile files('libs/YouTubeAndroidPlayerApi.jar') instead of compile project(':YouTubeAndroidPlayerApi').

I hope this can help you.

Upvotes: 5

mike
mike

Reputation: 561

I think maybe my gradle was out of sync. I also removed this line:

compile project(':YouTubeAndroidPlayerApi')

This is still there though:

compile files('libs/YouTubeAndroidPlayerApi.jar')

I could've sworn I tried it without the first line, but I can't say with absolute certainty.

Upvotes: 2

Related Questions