Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use YouTube Android Player API with AppCompatActivity

To play a video in my app I decided to extend from YouTube Android Player API. But the problem is my menu is disappeared because I'm not extending from AppCompatActivity. The question is: how to use YouTube Android Player API and have the menu in the app?

public class TutorialsActivity extends YouTubeBaseActivity {

private YouTubePlayerView youTubePlayerView;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.youtube);

    youTubePlayerView = (YouTubePlayerView) findViewById(R.id.video1);
    youTubePlayerView.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            youTubePlayer.loadVideo("c9q88492aas");
            youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
        }

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

        }
    });
}

XML

    <com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/video1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

When I extend from AppCompatActivity, it just gives me an error.

Error Log:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hyber.app/com.hyber.app.TutorialsActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class com.google.android.youtube.player.YouTubePlayerView

enter image description here

like image 283
JohnPix Avatar asked Jun 24 '18 18:06

JohnPix


1 Answers

Use YouTubePlayerFragment instead of using YouTubePlayerView. As the doc says:- A YouTubePlayerFragment is a fragment that contains a YouTubePlayerView. Using this fragment is the preferred way of playing YouTube videos because your activity does not need to extend an activity provided by the library, as is the case with using the YouTubePlayerView directly.

Find more info here

Activity code:-

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;

public class MainActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {

    private static final int RECOVERY_DIALOG_REQUEST = 1;

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

        YouTubePlayerFragment youTubePlayerFragment =
                (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
        youTubePlayerFragment.initialize("api key",
                this);
    }

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

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


}

Layout:-

<fragment
        android:id="@+id/youtube_fragment"
        android:name="com.google.android.youtube.player.YouTubePlayerFragment"
        android:layout_width="0dp"
        android:layout_height="300dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
like image 92
Nainal Avatar answered Nov 16 '22 00:11

Nainal