Friday 29 June 2018

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



from How to use YouTube Android Player API with AppCompatActivity

No comments:

Post a Comment