一个活动中有多个Youtube玩家

时间:2013-04-30 02:00:41

标签: android youtube youtube-api

我正在使用Youtube Android Player API,如下所示:https://developers.google.com/youtube/android/player/

但是,我不能同时在我的活动中收到多个视频。我尝试将两个YouTubePlayerViews放入活动中,如下所示:

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

当我尝试这个时,第一个视图只是黑色,而第二个视频加载。如果我在onCreate()中为第二个视频注释掉代码,那么只会加载第一个视频。

有没有办法让多个YouTubePlayerView进入同一个活动?

1 个答案:

答案 0 :(得分:2)

这可能为时已晚,但最近我遇到了同样的问题。希望这对可能面临同样问题的人有所帮助。解决这个问题的诀窍是使用YoutubePlayerFragments。只要您使用Youtube播放器视图,您就可以达到仅在多个请求中完成的一次初始化所施加的限制。通常,初始化成功进行最后一次。

我最终动态生成了gridlayout并将我(动态生成的)片段的视图放在每个单元格中。然后我在onResume中初始化播放器的实例。一旦初始化成功,您将获得播放器的句柄,并且您可以在片段上随机提示不​​同的视频。

我只需粘贴代码即可让您了解我如何构建片段并进行初始化:

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