微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么不调用 YouTubeSupportFragment.initialize()?

如何解决为什么不调用 YouTubeSupportFragment.initialize()?

我正在尝试在我的 Android 应用中实现 YouTube API。这是我的代码

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;

public class YouTubeFragment extends Fragment {
    YouTubePlayer player;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container,Bundle savedInstaceState) {
        return inflater.inflate(R.layout.youtube_fragment,container,false);
    }
    @Override
    public void onViewCreated(@NonNull View view,Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);
        Log.d("Failed","On View Created");
        YouTubePlayerSupportFragment youTubePlayerSupportFragment = YouTubePlayerSupportFragment.newInstance();
        assert getFragmentManager() != null;
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.ytFrame,youTubePlayerSupportFragment);
        Log.d("Failed","On View Created 2");
        youTubePlayerSupportFragment.initialize(YouTubeConfig.getApiKey(),new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider,YouTubePlayer youTubePlayer,boolean b) {
                if (!b) {
                    Log.d("Failed","Passed");
                    player = youTubePlayer;
                    player.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
                    player.loadVideo("WI5UTB_HVLg");
                    player.play();
                }
                else {
                    Log.d("Failed","b is true");
                }
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider,YouTubeInitializationResult youTubeInitializationResult) {
                Log.d("Failed","Failed");
            }
        });

    }
}

但 Logcat 仅显示 On View CreatedOn View Created 2。据我所知,出于某种原因既不调用 onInitializationSuccess( ) 也不调用 onInitializationFailure( )。谁能解释一下我哪里出错了?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。