Could not create instance of com.google.android.youtube.player.YouTubePlayerView

I am new to android development. I am trying to implement youtube Api. But in my XML file it shows the error "cannot be created: - com.google.android.youtube.player.YouTubePlayerView"

Here is my MAINActivity code: -

public class MainActivity extends YouTubeBaseActivity {
Button b;
private YouTubePlayerView youtubeplayerview;
private YouTubePlayer.OnInitializedListener onInitializedListener;


@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.activity_main);

    youtubeplayerview=(YouTubePlayerView)findViewById(R.id.Youtube_view);
    onInitializedListener=new YouTubePlayer.OnInitializedListener(){
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            youTubePlayer.loadVideo("wvjqFy33HVA");
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        }
    };
    b=(Button)findViewById(R.id.button);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            youtubeplayerview.initialize("API KEY",onInitializedListener);
        }
    });
}
}

Here is my XML file Code: -

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

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="PLAY VEDIO"
    android:id="@+id/button"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<com.google.android.youtube.player.YouTubePlayerView
    android:layout_width="500dp"
    android:layout_height="500dp"
    android:id="@+id/Youtube_view"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/button"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

Here is a screenshot of the error

+4
source share
2 answers

Have you added the YouTube API for Android to your library? By adding this, we fix the problem for me ... Try adding the same thing by following these steps:

1. jar API Android- YouTube https://developers.google.com/youtube/android/player/downloads/

2. zip , libs → YouTubeAndroidPlayerApi.jar

3. jar /app/libs/ Android.

, ('libs/YouTubeAndroidPlayerApi.jar') build.gradle

, ,

0

api- Android- youtube? : https://console.developers.google.com

, "API KEY" youtubeplayerview.initialize("API KEY",onInitializedListener); api, .

, YoutubeDeveloperKey - , :

public class ActivityPlayYouTube extends YouTubeBaseActivity implements
        YouTubePlayer.OnInitializedListener {

    private YouTubePlayer YPlayer;
    private static final String YoutubeDeveloperKey = "YOUR_DEVELOPER_KEY_GOES_HERE";
    private static final int RECOVERY_DIALOG_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playyoutube);

        YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        youTubeView.initialize(YoutubeDeveloperKey, this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.you_tube_api, menu);
        return true;
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider,
            YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format(
                    "There was an error initializing the YouTubePlayer",
                    errorReason.toString());
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    @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(YoutubeDeveloperKey, this);
        }
    }

    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return (YouTubePlayerView) findViewById(R.id.youtube_view);
    }

    @Override
    public void onInitializationSuccess(Provider provider,
            YouTubePlayer player, boolean wasRestored) {
        if (!wasRestored) {
            YPlayer = player;
            /*
             * Now that this variable YPlayer is global you can access it
             * throughout the activity, and perform all the player actions like
             * play, pause and seeking to a position by code.
             */
            YPlayer.cueVideo("2zNSgSzhBfM");
        }
    }
}
-3

All Articles