YoutubePlayer API 无限加载问题

YoutubePlayer API endless loading issue

每当发生新的地理围栏转换时,我都会开始一个新的意图。

GeofenceTransitionService.java中:

Intent intent = new Intent(GeofenceTransitionService.this, VideoActivity.class);
intent.putExtra("videoID", videoURLS[i]);
startActivity(intent);

我正在像这样初始化片段,

VideoActivity.java 中:


public class VideoActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {


    private String videoID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        videoID = getIntent().getStringExtra("videoID");
        YouTubePlayerFragment youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager()
                .findFragmentById(R.id.youtubePlayerFragment);

        youTubePlayerFragment.initialize("APIKEY", this);
    }

    
    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {

        youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION |
                YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);


        if(!wasRestored) {
            youTubePlayer.cueVideo(videoID);
        }
        youTubePlayer.setFullscreenControlFlags(0);
        youTubePlayer.setFullscreen(true);
        youTubePlayer.setShowFullscreenButton(false);
    }
    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error) {

        final int REQUEST_CODE = 1;

        if(error.isUserRecoverableError()) {
            error.getErrorDialog(this,REQUEST_CODE).show();
        } else {
            String errorMessage = String.format("There was an error initializing the YoutubePlayer (%1$s)", error.toString());
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        }
    }
}

activity_video.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".VideoActivity">

        <fragment
            android:id="@+id/youtubePlayerFragment"
            android:name="com.google.android.youtube.player.YouTubePlayerFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
</androidx.appcompat.widget.LinearLayoutCompat>

但是视频永远不会加载。不过,它在其他活动中效果很好。我已经检查了清单,但找不到任何可以区分这个 activity 和另一个有效的清单的东西。我对另一个 activity 使用相同的代码来初始化另一个片段。

我忘记拆分视频 url 以获取视频 ID。

intent.putExtra("videoID", videoURLS[i].split("=")[1]);