使用片段中的YouTube API [英] Using the YouTube API within a fragment

查看:109
本文介绍了使用片段中的YouTube API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用新的YouTube API在我的项目。使用文档的例子,它工作正常:

I using the new YouTube API in my project. Using the example of the documentation, it works correctly:

public class Test extends  YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

    String urlVideo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        urlVideo="5lbVNYAeFiQ";

        YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        youTubeView.initialize("MY_API_KEY", this);

    }

    public void onInitializationFailure(Provider arg0,YouTubeInitializationResult arg1) {
        Toast.makeText(this, "Error ", Toast.LENGTH_LONG).show();  
    }

    public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,boolean wasRestored) {
        if (!wasRestored) {
            arg1.loadVideo(urlVideo);
        }

    }
}

不过,在我的项目,我使用一个包含ViewPager主要活动 该ViewPager显示片段,问题是,当我想ViewPager的一个片段中显示YouTubePlayer。 通常情况下,使用YouTube的,扩展YouTubeBaseActivity的活动。

But in my project, I'm using a main activity that contains a ViewPager The ViewPager shows fragments, the problem is when I want to show YouTubePlayer within one fragment of ViewPager. Normally, an activity that uses youtube, extending YouTubeBaseActivity.

extends YouTubeBaseActivity implements ... { 

我的问题是,我怎么可以从片段扩展活动显示YouTube播放器?

My question is, how I can show YouTube Player in an activity that extends from fragments?

我搜查,但找不到关于我的问题的信息。

I searched, but can not find information about my problem.

我衷心AP preciate的帮助。

I sincerely appreciate the help.

最好的问候

推荐答案

使用<一个href="https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayerSupportFragment"相对=nofollow> YouTubePlayerSupportFragment 如果你只在一个support.v4.app.Fragment播放YouTube视频。这使您可以使用FragmentActivity,而不是YouTubeBaseActivity。

Use YouTubePlayerSupportFragment if you are only playing a YouTube video in a single support.v4.app.Fragment. This allows you to use FragmentActivity, rather than YouTubeBaseActivity.

这篇关于使用片段中的YouTube API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆