如何对扩展YouTubePlayerSupportFragment类的片段进行多个实例化?

时间:2015-05-20 19:36:37

标签: android-youtube-api

我的主Activity中有两个片段。 每个片段都应播放不同的YouTube视频。 但是在我的代码中,虽然我将不同的URL传递给了YouTubePlayerSupportFragment类的两个实例,但两个片段都在播放相同的视频。

MainActivity -

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    VideoFragment f1=VideoFragment.newInstance("kXYiU_JCYtU");

    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container1, f1).commit();
    VideoFragment f2 =  new VideoFragment();
    f2=VideoFragment.newInstance("k4V3Mo61fJM");
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container2, f2).commit();
}

activity_main.xml中 -

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
>

<FrameLayout
    android:id="@+id/fragment_container1"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    >
</FrameLayout>

<FrameLayout
    android:id="@+id/fragment_container2"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/fragment_container1"
    >
</FrameLayout>
</RelativeLayout>

VideoFragment类 -

public VideoFragment() { }

public static VideoFragment newInstance(String url) {

    VideoFragment f = new VideoFragment();

    Bundle b = new Bundle();
    b.putString("url", url);

    f.setArguments(b);
    f.init();

    return f;
}

private void init() {

    initialize("AIzaSyDM2vaVDpwGGFEcQCcD_hA38ZQo2Tqa06o", new YouTubePlayer.OnInitializedListener() {



        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
            if (!wasRestored) {
                player.cueVideo(getArguments().getString("url"));
            }
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        }


    });
}

如果有人能帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

不可能。 Youtube播放器基于单声道模式,所以每次你都会得到相同的播放器