如何将youtube集成到片段?

时间:2017-05-16 11:35:10

标签: android android-fragments youtube-api

我正在尝试实现你的片段管。在我成功实现你在一个活动中管它完美的工作,但此时在片段中显示黑屏。我尝试(Integrating youtube to fragment

片段代码: -

    public class YoutubeFragment extends Fragment {

private MainActivity myContext;

private YouTubePlayer YPlayer;
private String mVideoId = "QdUoieQOZ2s";

private static final String YoutubeDeveloperKey = "AIzaSyBVKvfrmSBUaCsj850v2Ks-2FCWiXn_rEw";

@Override
public void onAttach(Activity activity) {

    if (activity instanceof MainActivity) {
        myContext = (MainActivity) activity;
    }

    super.onAttach(activity);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.inflate, container, false);

    YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

    youTubePlayerFragment.initialize(YoutubeDeveloperKey,
            new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                    if (!b) {
                        YPlayer = youTubePlayer;
                        YPlayer.setShowFullscreenButton(false);
                        YPlayer.cueVideo(mVideoId);
                    }
                }

                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

                }
            });
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.fl_youtube, youTubePlayerFragment).commit();
    return rootView;
}

}

活动类: -

public class MainActivity extends AppCompatActivity {

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

}

膨胀Xml文件: -

<FrameLayout
    android:id="@+id/fl_youtube"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"></FrameLayout>

1 个答案:

答案 0 :(得分:0)

片段代码

private YouTubePlayer mYoutubePlayer;
private String mVideoId;

//Call this method after getting mVideoId
private void initializeYoutubeFragment() {

    YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

    youTubePlayerFragment.initialize(getString(R.string.youtube_key), new YouTubePlayer.OnInitializedListener() {

        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
             if (!wasRestored) {
                mYoutubePlayer = player;
                mYoutubePlayer.setShowFullscreenButton(false);
                mYoutubePlayer.cueVideo(mVideoId);
             }

        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {


        }
    });
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.fl_youtube, youTubePlayerFragment).commit();
}


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    initializeYoutubeFragment();
}

和XML

....<!--Put his frame layout wherever you need in fragment view xml-->
<FrameLayout
         android:id="@+id/fl_youtube"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:visibility="gone">
....