Youtube API 的解决方法? [英] Workaround for Youtube API?
问题描述
我知道之前可能有人问过这个问题,但这是我参与的一个特殊案例..
I know this might have been asked before, but this is a special case I am involved into..
重要事项我需要在一个单独的类中执行此操作,而不是一个活动.其实我的班级继承了别人写的 BaseViewPagerAdapter ..
IMPORTANT I need to do this inside a separate class, not an activity. Actually my class extends a BaseViewPagerAdapter wrote by someone else..
这个想法是,我不能使用 YoutubePlayerView,因为我不能扩展 YoutubeBaseActivity.
The idea is that, I can't use YoutubePlayerView because I can not extend YoutubeBaseActivity.
我也尝试过使用 YoutubePlayerSupportFragment,但同样不起作用,因为我无法从我的 XML 文件中获取 ......
I also tried using YoutubePlayerSupportFragment, same not working because I can't get the refernce for the from my XML file...
我确实尝试过这样的事情
I did try something like this
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 的类中创建了一个实例并尝试这样做
And I made an instance of this into the class that extends BaseViewPageAdapter and tried to do
YouTubePlayerSupportFragment youtube = YouTubePlayerSupportFragment.newInstance();
youtube.getFragmentManager().beginTransaction().replace(R.id.youtubeplayerfragment,youtube);
但是我收到一个错误,即替换函数应用于空对象引用..
But there I get an error that replace function is applied on a null object reference ..
如果您有任何建议或想法我可以如何让它在这个特殊情况下工作,我将不胜感激
If you have any suggestion, or idea how I could make it work on this particular case I would be grateful
推荐答案
好的,我设法让它在我的上下文中像这样工作
Ok, I managed to make it work on my context like this
首先,我将我的 YTVideoFragment 类更改为扩展 Fragment
First of all, I changed my YTVideoFragment class to extends 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;
}
}
并在我需要的地方创建了该类的实例
And made an instance of that class where I needed it like this
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();
I have to mention here, that being inside a BaseViewPagerAdapter I had to cast my context to (FragmentActivity) in order to be able to call getSupportFragmentManager();
将替换我的主 XML 文件中的 VideoView 的帧布局 XML 是这样的:
Frame layout XML which will replace a VideoView inside my main XML file is this:
<?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>
这篇关于Youtube API 的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!