7

I'm using the Youtube Android Player API as outlined here: https://developers.google.com/youtube/android/player/

However, I can't get more than one video into my activity at once. I tried simply putting two YouTubePlayerViews into the activity like so:

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

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

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

</LinearLayout>

MainActivity.java

package com.example.multidemo;

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.view_one)).initialize("API key", new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1, boolean arg2) {
                arg1.cueVideo("RpwoN_XlN6Y");
            }

            @Override
            public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
            }
        });

        ((YouTubePlayerView) findViewById(R.id.view_two)).initialize("API key", new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1, boolean arg2) {
                arg1.cueVideo("jkk2mMq2x8E");
            }

            @Override
            public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
            }
        });
    }
}

When I try this, the first view just comes up black, while the second video loads. If I comment out the code in onCreate() for the second video, then just the first video will load.

Is there any way to get multiple YouTubePlayerViews into the same activity?

Norman Lee
  • 389
  • 3
  • 10

1 Answers1

3

This might be too late for an answer, but i had the same issue recently. Hope this helps to someone who might face the same issue. The trick to get around this is Using YoutubePlayerFragments instead. As long as you use Youtube player view, you'll hit the limit imposed of only one initialization done out of multiple requests. Typically the initialization succeeds for last one.

I ended up dynamically generating gridlayout and placing my (dynamically generated)fragment's view in each of the cells. And then i initialize the instance of player inside onResume. Once initialization is sucessful, you'll get a handle to player, and you can cue different videos randomly on fragments.

I'll just paste the code that will give you an idea about how i am constructing fragments and doing initialization:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{
    mLogger.i("Preparing Youtube view for fragment ..");
    mFragmentHeight = CustomFragment.getFragmentHeight(mNumScreens,mNumColumns);
    mFragmentWidth = CustomFragment.getFragmentWidth(mNumColumns);

    LinearLayout linearLayout = new LinearLayout(getActivity());
    GridLayout.LayoutParams gridparams = new GridLayout.LayoutParams();
    gridparams.rowSpec = GridLayout.spec(mGridRow);
    gridparams.columnSpec = GridLayout.spec(mGridColumn);
    gridparams.height = mFragmentHeight;
    gridparams.width = mFragmentWidth;
    linearLayout.setLayoutParams(gridparams);
    if(YouTubeApiServiceUtil.isYouTubeApiServiceAvailable(getActivity())==
            YouTubeInitializationResult.SUCCESS)
    {
        View ytView = super.onCreateView(inflater, container, savedInstanceState);
        linearLayout.addView(ytView);
    }
    else
        mLogger.e("Either youtube service is down," +
                " or you dont have required version of YouTube app .." +
                "\nYouTube Fragment will not work as expected..");
    mView = linearLayout;
    mLogger.i("Preparation youtube view for fragment complete..");
    return mView;
}

@Override
public void onInitializationFailure(Provider arg0,
        YouTubeInitializationResult arg1) 
{
    mLogger.e("Youtube player initialization failed");
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
          boolean wasRestored) 
{
    mLogger.i("You tube player initialized successfully.. will attempt to stream..");
    mYoutubePlayer = player;
    setListeners();
    try
    {
        if (!wasRestored) 
        {
            if(mPlayListFragment)
              player.loadPlaylist(getResourceID());
            else
                player.loadVideo(getResourceID());
        }
    }
    catch(Exception e)
    {
        mLogger.e("Error loading playlist/video .."+e);
    }

}

public void onResume()
{
    super.onResume();
    initialize(API_KEY, this);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
GodOnScooter
  • 351
  • 3
  • 7
  • Could you show me some of your code? I'm trying to put a YoutubePlayerFragment in a gridlayout too. – pldimitrov Jun 05 '14 at 15:53
  • I'll just paste the code that will give you an idea about how i am constructing fragments and doing initialization. See my edit above for an idea. Mind you it's not a complete answer, just an idea that might help someone facing the issue – GodOnScooter Aug 01 '14 at 23:15
  • hello. i have the similar thing but the problem is the screen stays blank.. even the initialization is successful and it start playing video.. but i cant see any video preview. can you help how to show that.or share some more code. thanks. – Mustanser Iqbal Mar 10 '16 at 13:42
  • Hello! I am facing a problem of all the players displaying the lastly loaded video. "Multiple YouTubeFragments in a HorizontalScrollView". Here's the link to my question. https://stackoverflow.com/questions/38400015/multiple-youtubeplayerfragments-inside-a-horizontal-scrollview-all-the-players. It would be great if you could help! – jQueen Jul 15 '16 at 15:57