使用youtube播放器API显示和播放多个视频

时间:2017-06-15 05:11:51

标签: android video youtube youtube-api

我偶然发现使用youtube API播放超过1个视频的问题。

我发现了这篇文章:Playing different youtube videos using Youtube Player API

问题:

我无法通过在同一页面上保留多个玩家来解决它是如何解决的。

我正在尝试使用2个不同的视频代码初始化2个播放器,但是当我 尝试播放1个视频,两个都运行,然后1秒后,两个都停止了。

我该如何解决?谁能指导我?

这是我的代码:

public class YoutubeTvFragment extends Fragment {

    private static final String LOG = "Youtube TV";

    private FragmentActivity myContext;
    private YouTubePlayer YPlayer, YPlayer2;
    private static final String YoutubeDeveloperKey = "";
    private static final int RECOVERY_DIALOG_REQUEST = 1;

    @Override
    public void onAttach(Activity activity) {

        if (activity instanceof FragmentActivity) {
            myContext = (FragmentActivity) activity;
        }

        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        // Defines the xml file for the fragment
        View rootView = inflater.inflate(R.layout.fragment_tv_youtube, container, false);



        YouTubePlayerSupportFragment youTubePlayerSupportFragment = YouTubePlayerSupportFragment.newInstance();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.youtube_fragment, youTubePlayerSupportFragment).commit();

        youTubePlayerSupportFragment.initialize(YoutubeDeveloperKey, new OnInitializedListener() {

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider arg0, YouTubePlayer youTubePlayer, boolean b) {
                if (!b) {
                    YPlayer = youTubePlayer;
                    YPlayer.cueVideo("eCdXcZeKgxU");

                    //YPlayer.setShowFullscreenButton(false);
                    //YPlayer.pause();
                    //YPlayer.play();
                }
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
                // TODO Auto-generated method stub

            }
        });



        YouTubePlayerSupportFragment youTubePlayerSupportFragment2 = YouTubePlayerSupportFragment.newInstance();
        FragmentTransaction transaction2 = getChildFragmentManager().beginTransaction();
        transaction2.add(R.id.youtube_fragment2, youTubePlayerSupportFragment2).commit();

        youTubePlayerSupportFragment2.initialize(YoutubeDeveloperKey, new OnInitializedListener() {

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider arg0, YouTubePlayer youTubePlayer, boolean c) {
                if (!c) {
                    YPlayer2 = youTubePlayer;
                    YPlayer2.cueVideo("jgnGD74BKoA");

                    //YPlayer2.setShowFullscreenButton(false);
                    //YPlayer2.pause();
                    //YPlayer2.play();
                }
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
                // TODO Auto-generated method stub

            }


        });



        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }
}

0 个答案:

没有答案