【问题标题】:How to perform multiple instantiation of a fragment extending YouTubePlayerSupportFragment class?如何执行扩展 YouTubePlayerSupportFragment 类的片段的多个实例化?
【发布时间】:2015-05-20 19:36:36
【问题描述】:

我的主 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) {

        }


    });
}

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

【问题讨论】:

    标签: android-youtube-api


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 2013-05-24
      • 2016-06-19
      • 2021-02-19
      • 1970-01-01
      • 2013-07-04
      相关资源
      最近更新 更多