Android youtube API play two or more Youtube players in one main activity
Asked Answered
Z

2

9

Using the Android Youtube api i want to play two videos in my app main activity without using fragment we have the same problem with this Multiple Youtube players in one activity but has no acceptable answers yet please help us.

activity_main.xml

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

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

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

MainActivity.java

    package com.apps.you;
    import android.os.Bundle;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

public class MainActivity extends YouTubeBaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    ((YouTubePlayerView) findViewById(R.id.video1)).initialize("AIzaSyBmb9Yqu9vEBXjNOrBmZZ__6s12RH5RSv0", new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(Provider provider1, YouTubePlayer player1, boolean restored1) {
            if (!restored1) {
            player1.cueVideo("Daa38ruXHxE");
            }
        }

        @Override
        public void onInitializationFailure(Provider provider1, YouTubeInitializationResult result1) {
        }
    });

    ((YouTubePlayerView) findViewById(R.id.video2)).initialize("AIzaSyBmb9Yqu9vEBXjNOrBmZZ__6s12RH5RSv0", new YouTubePlayer.OnInitializedListener() {

        public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean restored) {

            if (!restored) {
        player.loadVideo("ctQAPiojDKE");
            }
        }


        public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
        }
    });
}
}

the problem with this is only one video was playing at the end , video1 initialization was overrriden by the video2 initialization they call the same

  .initialize(new YouTubePlayer.OnInitializedListener(){});

by this:

((YouTubePlayerView) findViewById(R.id.video1)).initialize(new
    YouTubePlayer.OnInitializedListener(){});

((YouTubePlayerView) findViewById(R.id.video2)).initialize(new
    YouTubePlayer.OnInitializedListener(){});
Zwick answered 5/3, 2014 at 6:1 Comment(2)
Did you solve this issue?Bodice
Eliran Tutia as far as I remember I can only play and initialize one video at a time per activity even using fragments.Zwick
H
0

Maybe trivial answer. But is for your use-case acceptable to call init of second youtube player from success callback of the first one? Have you tried it? It should be a reasonable workaround.

But there might be a catch that is extending YouTubeBaseActivity I looked up documentation, and it looks like there can be only one YouTubeView per activity because of binding between them.

So I can offer another advice, use YouTubePlayerFragment or SupportYouTubePlayerFragment instead. It should have its own lifecycle (not tested).

Hillinck answered 30/6, 2014 at 15:3 Comment(2)
tried to call init of second youtube player from success callback of the first one. doesn't work due to limitation =(Synthesis
using youtubeplayerfragments don't allow it to work eitherSynthesis
A
0

To achieve two or more youtube players at one activity you need to use YouTubeThumbnailView(It is an ImageView) to show a list of preview images. When the user touch the thumb, you redirect to full screen video player.

Check out this project.

Aramenta answered 23/10, 2016 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.