Skip to content
Advertisement

YoutubePlayer API endless loading issue

I am starting a new intent whenever a new geofence transition happens.

In GeofenceTransitionService.java:

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

And I am initializing the fragment like this,

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

But the video will never load. It works fine on other activities though. I have checked the manifest but couldn’t find anything that differentiates this activity from the other one that works. I use the same code for the other activity to initialize the other fragment.

Advertisement

Answer

I forgot to split the video url to get the videoID.

intent.putExtra("videoID", videoURLS[i].split("=")[1]);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement