我知道之前可能会问这个问题,但这是我参与的一个特例..
重要的 我需要在一个单独的类中执行此操作,而不是活动。实际上我的类扩展了由其他人写的BaseViewPagerAdapter ..
我的想法是,我无法使用YoutubePlayerView,因为我无法扩展YoutubeBaseActivity。
我也尝试过使用YoutubePlayerSupportFragment,同样没有用,因为我无法从我的XML文件中获取引用...
我确实尝试过这样的事情
public class YTVideoFragment extends YouTubePlayerSupportFragment {
private final static String TAG = "YoutubeVideoFragment";
private static final String DEV_KEY = "key";
private static String id;
private YouTubePlayer mPlayer;
public static YTVideoFragment newInstance(String url){
YTVideoFragment videoFragment = new YTVideoFragment();
Bundle bundle = new Bundle();
bundle.putString("uri", url);
videoFragment.setArguments(bundle);
videoFragment.init(0);
Log.i(TAG, "new Instance FINAL ");
return videoFragment;
}
public void init(final int time) {
initialize(DEV_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
mPlayer = youTubePlayer;
Log.i(TAG, "-=onInitializationSuccess=-");
mPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
id = getArguments().getString("url");
if (!b){
mPlayer.loadVideo(id, time);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.i(TAG, "-=onInitializationFailure=-");
Log.e(TAG, youTubeInitializationResult.toString());
}
});
}
}
我在扩展BaseViewPageAdapter并尝试执行
的类中创建了一个实例YouTubePlayerSupportFragment youtube = YouTubePlayerSupportFragment.newInstance();
youtube.getFragmentManager().beginTransaction().replace(R.id.youtubeplayerfragment,youtube);
但是我得到一个错误,即替换函数应用于空对象引用..
如果您有任何建议或想法如何让它能够在这个特定案例中发挥作用我将不胜感激
答案 0 :(得分:0)
好的,我设法让它在我的上下文中工作
首先,我更改了YTVideoFragment类以扩展Fragment
public class YoutubeFragment extends Fragment {
private static final String API_KEY = DateUtils.getDeveloperKey();
private static String VIDEO_ID;
public void setVideoID(String id){
VIDEO_ID = id;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.youtube_layout, container, false);
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(API_KEY, new OnInitializedListener() {
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
player.loadVideo(VIDEO_ID);
player.play();
}
}
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) {
// YouTube error
String errorMessage = error.toString();
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.d("errorMessage:", errorMessage);
}
});
return rootView;
}
}
并且像我这样需要它的那个类的实例
YoutubeFragment youtubeFragment = new YoutubeFragment();
youtubeFragment.setVideoID(eventData.getYoutube_id());
FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.relativeLayout, youtubeFragment)
.commit();
我必须在这里提一下,在BaseViewPagerAdapter中我必须将我的上下文转换为(FragmentActivity)才能调用getSupportFragmentManager();
框架布局XML将取代我的主XML文件中的VideoView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/youtube_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:visibility="visible" />
</RelativeLayout>