У меня есть два фрагмента в моей основной деятельности. Каждый фрагмент должен воспроизводить разные видео на YouTube. Но в моем коде оба фрагмента воспроизводят одно и то же видео, хотя я передаю разные URL-адреса двум экземплярам класса YouTubePlayerSupportFragment.
Основная деятельность-
@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) {
}
});
}
Я был бы признателен, если бы кто-нибудь мог мне помочь.