【问题标题】:Android YouTube API: Can't reinitialize YouTubePlayerFragment with new YouTube videoAndroid YouTube API:无法使用新的 YouTube 视频重新初始化 YouTubePlayerFragment
【发布时间】:2014-08-21 13:30:41
【问题描述】:

单击按钮时,我正在尝试将不同的视频加载到 YoutubePlayer 片段中。我的按钮有一个 testClick 的 onclick 监听器。当我单击按钮时,出现以下异常:“UsupportedException:无法在播放器顶部添加视图”。当我从我的 onclick 方法初始化新视频时,也不会调用 initialize 方法。如何将新的 YouTubeVideo 加载到以前使用的 YouTubePlayerFragment 中,以及如何从我的 onClick 方法中调用初始化方法?

public class ExersizeListActivity extends Activity implements
    YouTubePlayer.OnInitializedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exersize_list);
}

public void testClick(View v) {
    //new YouTube Fragment to replace old one
    YouTubePlayerFragment youTubePlayerFragment=YouTubePlayerFragment.newInstance();
    youTubePlayerFragment.initialize(YoutubeAPIKey.API_KEY, this);

    FragmentManager fragManager=getFragmentManager();
    FragmentTransaction fragmentTransaction=fragManager.beginTransaction();


    fragmentTransaction.replace(R.id.youtube_fragment, youTubePlayerFragment);
    fragmentTransaction.commit();       
}

@Override
public void onInitializationFailure(Provider arg0,
        YouTubeInitializationResult arg1) {
    Log.e(null, "it bombed");

}

//not being called, and in current state would reinitialize with the same video
@Override
public void onInitializationSuccess(Provider arg0,
        YouTubePlayer youtubePlayer, boolean arg2) {
    youtubePlayer.cueVideo("YNKehLXpLRI");      
}

}

<RelativeLayout

    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <fragment
        android:id="@+id/youtube_fragment"
        android:name="com.google.android.youtube.player.YouTubePlayerFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
    android:id="@+id/randomizeButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Randomize!" 
    android:layout_below="@+id/youtube_fragment"
    android:onClick="testClick"/>
</RelativeLayout> 

【问题讨论】:

    标签: android youtube-api android-youtube-api youtube-data-api


    【解决方案1】:

    不要在xml布局文件中使用&lt;fragment&gt;标签定义片段,而是定义一个FrameLayout容器,在runtime动态更改片段时需要:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
        <FrameLayout
            android:id="@+id/youtube_fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/randomizeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Randomize!" 
            android:layout_below="@+id/youtube_fragment"
            android:onClick="testClick"/>
    </RelativeLayout> 
    

    您需要在onCreate() 中添加第一个YouTubeVideoFragment 和FragmentTransaction,当用户单击该按钮时,您在testClick() 中已有的代码应该可以工作。

    【讨论】:

      猜你喜欢
      • 2012-09-19
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2015-12-23
      • 2020-04-20
      • 2017-07-09
      • 1970-01-01
      • 2020-12-07
      相关资源
      最近更新 更多