Youtube API 的解决方法?



我知道以前可能有人问过这个问题,但这是我参与的一个特例。

重要提示我需要在单独的类中执行此操作,而不是在活动中执行此操作。实际上,我的类扩展了其他人编写的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);

但是在那里我收到一个错误,即替换函数应用于空对象引用..

如果您有任何建议,或者知道我如何使其适用于此特定情况,我将不胜感激

好的,我设法让它像这样在我的上下文中工作

首先,我更改了我的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文件中的VideoView的帧布局XML是这样的:

<?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> 

相关内容

  • 没有找到相关文章

最新更新