片段彻底摧毁YouTube

时间:2015-10-07 22:18:02

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

当我第一次加载YouTube时效果很好,但是当我尝试重新加载页面时,yotube总是在等待......

这个错误是因为“Fragment”没有完全破坏。如果你关闭App,第一次运作良好。

我的问题是如何彻底摧毁片段?

public class YoutubeFragment extends Fragment {
    // API キー
    private static final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    // YouTubeのビデオID
    private static String VIDEO_ID = "EGy39OMyHzw";

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

        // YouTubeフラグメントインスタンスを取得
        YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

        // レイアウトにYouTubeフラグメントを追加
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit();

        // YouTubeフラグメントのプレーヤーを初期化する
        youTubePlayerFragment.initialize(API_KEY, new OnInitializedListener() {

            // YouTubeプレーヤーの初期化成功
            @Override
            public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
                if (!wasRestored) {
                    player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                    player.loadVideo(VIDEO_ID);
                    player.play();
                }
            }

            // YouTubeプレーヤーの初期化失敗
            @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;
    }
}

YouTube简单:https://gist.github.com/takeshiyako2/e776bbaf2966c6501c4f

相关:Android Youtube api View Pager issue

0 个答案:

没有答案