如何发挥使用YouTubePlayerFragment在YouTube API的视频? [英] How to play a video with the YouTube API using YouTubePlayerFragment?

查看:907
本文介绍了如何发挥使用YouTubePlayerFragment在YouTube API的视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的片段播放视频。但是,我无法得到它的工作。如果我'延伸YoutubeFailureRecovery'我得到:

  

09-06 21:56:40.472:E / AndroidRuntime(4946):java.lang.IllegalStateException:所致。一个YouTubePlayerView只能与延伸YouTubeBaseActivity作为其上下文中的活动创建


 < com.google.android.youtube.player.YouTubePlayerView
    机器人:ID =@ + ID /播放器
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT/>



公共类YoutubeFragment扩展YoutubeFailureRecovery {

    YouTubePlayer播放器;
    YouTubePlayerView playerView;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑ARG2){
        // TODO自动生成方法存根
        返回inflater.inflate(R.layout.detailview,集装箱,假);
    }

    @覆盖
    公共无效的OnStart(){
        // TODO自动生成方法存根
        super.onStart();
        playerView =(YouTubePlayerView)getActivity()findViewById(R.id.player)。
        playerView.initialize(DataHolder.DEVELOPER_KEY,这一点);

    }

    @覆盖
    公共无效onInitializationSuccess(供应商提供,YouTubePlayer球员,
                                        布尔wasRestored){
        // TODO自动生成方法存根
        player.cueVideo(nCgQDjiotG0);
    }

    @覆盖
    保护提供商getYouTubePlayerProvider(){
        // TODO自动生成方法存根
        返回playerView;
    }
}
 


 公共抽象类YoutubeFailureRecovery扩展YouTubePlayerSupportFragment实现YouTubePlayer.OnInitializedListener {
 

我只是不知道该怎么办。我试图用YoutubePlayerSupportFragment致以片段班,尝试添加一个类似视频的片段XML,但没有什么工作(错误膨胀)。也许有人与使用YouTube API片段中的一些经验?

解决方案
  

该YouTubePlayerView只能与的活动扩展   YouTubeBaseActivity。

如果你想使用片段您必须使用YoutubePlayerFragment / SupportFragment。

例如,您可以创建自定义片段,它继承YoutubePlayerSupportFragment:

 公共类VideoFragment扩展YouTubePlayerSupportFragment {

    公共VideoFragment(){}

    公共静态VideoFragment的newInstance(字符串URL){

        VideoFragment F =新VideoFragment();

        叠B =新包();
        b.putString(URL,网址);

        f.setArguments(B);
        f.init();

        返回F;
    }

    私人无效的init(){

        初始化(yourapikey,新OnInitializedListener(){

            @覆盖
            公共无效onInitializationFailure(供应商为arg0,YouTubeInitializationResult ARG1){}

            @覆盖
            公共无效onInitializationSuccess(YouTubePlayer.Provider提供商,YouTubePlayer球员,布尔wasRestored){
                如果(!wasRestored){
                    player.cueVideo(getArguments()的getString(URL));
                }
            }
        });
    }
}
 

在code也可能是这样的:

  VideoFragment F = VideoFragment.newInstance(您的视频-URL);
。getSupportFragmentManager()的BeginTransaction()代替(R.id.fragment_container,F).commit()。
 

fragment_container ,在这种情况下,应该是一个空的的FrameLayout

I'm trying to play video in my Fragment. However, I cannot get it to work. If I'm extending 'YoutubeFailureRecovery' I get:

09-06 21:56:40.472: E/AndroidRuntime(4946): Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.


<com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/player"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>



public class YoutubeFragment extends YoutubeFailureRecovery {

    YouTubePlayer player;
    YouTubePlayerView playerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle arg2) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.detailview, container, false);
    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        playerView = (YouTubePlayerView)getActivity().findViewById(R.id.player);
        playerView.initialize(DataHolder.DEVELOPER_KEY, this);

    }

    @Override
    public void onInitializationSuccess(Provider provider, YouTubePlayer player,
                                        boolean wasRestored) {
        // TODO Auto-generated method stub
        player.cueVideo("nCgQDjiotG0");
    }

    @Override
    protected Provider getYouTubePlayerProvider() {
        // TODO Auto-generated method stub
        return playerView;
    }
}


public abstract class YoutubeFailureRecovery extends YouTubePlayerSupportFragment implements YouTubePlayer.OnInitializedListener{

I just don't know what to do. I tried to extend my Fragment class with 'YoutubePlayerSupportFragment', tried to add a video-like fragment in XML, but nothing is working (error inflating). Maybe someone has some experience with using YouTube API in Fragment?

解决方案

The YouTubePlayerView only works with an Activity that extends YouTubeBaseActivity.

If you want to use Fragments you have to use the YoutubePlayerFragment / SupportFragment.

You can for example create your custom Fragment that inherits from YoutubePlayerSupportFragment:

public class VideoFragment extends YouTubePlayerSupportFragment {

    public VideoFragment() { }

    public static VideoFragment newInstance(String url) {

        VideoFragment f = new VideoFragment();

        Bundle b = new Bundle();
        b.putString("url", url);

        f.setArguments(b);
        f.init();

        return f;
    }

    private void init() {

        initialize("yourapikey", new OnInitializedListener() {

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

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
                if (!wasRestored) { 
                    player.cueVideo(getArguments().getString("url"));
                }
            }
        });
    }
}

In code it could look like this:

VideoFragment f = VideoFragment.newInstance("your-video-url");   
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).commit();

The "fragment_container" in this case should be an empty FrameLayout.

这篇关于如何发挥使用YouTubePlayerFragment在YouTube API的视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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