如何使用YouTubePlayerFragment使用YouTube API播放视频?

时间:2013-09-06 19:11:27

标签: android android-fragments android-youtube-api

我正试图在片段中播放视频。但是,我无法让它发挥作用。如果我正在扩展'YoutubeFailureRecovery',我会得到:

  

09-06 21:56:40.472:E / AndroidRuntime(4946):引起:java.lang.IllegalStateException:只能使用扩展YouTubeBaseActivity作为其上下文的Activity创建YouTubePlayerView。

这是我的.xml:

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

我只是不知道该怎么做。我尝试使用'YoutubePlayerSupportFragment'扩展我的Fragment类,尝试在XML中添加类似视频的片段,但没有任何工作(错误膨胀)。也许有人在Fragment中使用YouTube API有一些经验?

1 个答案:

答案 0 :(得分:28)

  

YouTubePlayerView仅适用于扩展的活动   YouTubeBaseActivity。

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

例如,您可以创建从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"));
                }
            }
        });
    }
}

在代码中它看起来像这样:

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

&#34; fragment_container &#34;在这种情况下应该是一个空 FrameLayout