How to use YouTube Android Player API with AppCompatActivity
Asked Answered
E

2

7

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

Emeraldemerge answered 24/6, 2018 at 18:14 Comment(4)
what's the reason for not extending AppCompatActivity?Perse
@PierfrancescoSoffritti it crashes, I added logs in description .Emeraldemerge
@Nikolai Please have a look at my answer here.Watters
I also tried this earlier but was not able to do so, so I implemented this in a dialog let me know if you want help in implementing it in a dialog.Spadix
S
11

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" />
Seto answered 3/7, 2018 at 11:52 Comment(0)
S
3

Make use of App AppCompatDelegate,

for example:

1)Add AppCompatDelegate to your Activity

public class MainActivity extends Activity implements AppCompatCallback {

    @Override
    public void onSupportActionModeStarted(ActionMode mode) {
      //let's leave this empty, for now
    }

    @Override
    public void onSupportActionModeFinished(ActionMode mode) {   
      // let's leave this empty, for now
    }

Then, in the onCreate() of our Activity:

i) Create the AppCompatDelegate with AppCompatDelegate.create()

ii) Call AppCompatDelegate.onCreate() (There are some Activity lifecycle methods which should be proxied to the delegate)

iii) Inflate the layout with AppCompatDelegate.setContentView()

iv) Add the Toolbar to the delegate with AppCompatDelegate.setSupportActionbar()

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

    //let's create the delegate, passing the activity at both arguments (Activity, AppCompatCallback)
    delegate = AppCompatDelegate.create(this, this);

    //we need to call the onCreate() of the AppCompatDelegate
    delegate.onCreate(savedInstanceState);

    //we use the delegate to inflate the layout
    delegate.setContentView(R.layout.activity_main);

    //Finally, let's add the Toolbar
    Toolbar toolbar= (Toolbar) findViewById(R.id.my_awesome_toolbar);
    delegate.setSupportActionBar(toolbar);
}

For more details,

https://medium.com/google-developer-experts/how-to-add-toolbar-to-an-activity-which-doesn-t-extend-appcompatactivity-a07c026717b3

Hope it may help you.

Sheepish answered 27/6, 2018 at 4:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.