Multiple Youtube players in one activity

2019-04-07 17:17发布

问题:

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?

回答1:

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);
}