【发布时间】: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) {
}
});
}
如果有人可以帮助我,我将不胜感激。
【问题讨论】: