Home » Android » Display Multiple Videos in Multiple Fragment In One Activity Using Youtube Player API

Display Multiple Videos in Multiple Fragment In One Activity Using Youtube Player API

Posted by: admin November 1, 2017 Leave a comment

Questions:

All I want is My videos to be normally loaded when anyone clicks on the video icon in the video control panel, and the video is afterwards played.

I am stuck. I am able to load one video but I am unable to load multiple videos in one activity sequence.

I want to load the next:

  • fragment names from an array
  • videos id from an array

So that I can load many videos in one activity.

Here is my code:

activity_video.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.soleyman.happybirthdayurj.VideoActivity">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Button
                android:id="@+id/unlisted"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Info"
                android:onClick="unlistClick"
                />

            <fragment
                android:id="@+id/youtube_player_fragment1"
                android:name="com.google.android.youtube.player.YouTubePlayerFragment"
                android:layout_width="280dp"
                android:layout_height="150dp"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"/>

            <fragment
                android:id="@+id/youtube_player_fragment2"
                android:name="com.google.android.youtube.player.YouTubePlayerFragment"
                android:layout_width="280dp"
                android:layout_height="150dp"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"/>



        </LinearLayout>


    </ScrollView>
</RelativeLayout>

VideoActivity.java

    package com.example.soleyman.happybirthdayurj;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

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

public class VideoActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {

    private YouTubePlayer sPlayer;
    YouTubePlayerFragment playerFragment;

    private String[] ytNames = {"R.id.youtube_player_fragment1", "R.id.youtube_player_fragment2"};
    private String[] vdoID = {"PiKxS3P5o2Y", "PwC-fFIGjZM"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Show Actionbar
        ActionBar actionBar = getSupportActionBar();
        actionBar.show();

        this.setTitle("My Favorite Panda Videos");
        setContentView(R.layout.activity_video);


        playerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(ytNames[]); // currently it's showing error
        playerFragment.initialize(PlayerConfig.API_KEY, this);






    }

    private void playVideo(String videoId) {
        if ( sPlayer != null ) {
            sPlayer.loadVideo(videoId);
            sPlayer.cueVideo(videoId);
        }
    }


    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
        sPlayer = youTubePlayer;
        playVideo(vdoID[]); // currently it's showing error

    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        sPlayer = null;
    }



    // UnlistedActivity Activity
    public void unlistClick(View view) {
        Intent intent = new Intent(VideoActivity.this, UnlistedActivity.class);
        startActivity(intent);
    }
}

Any help is highly appreciated. If you have any suggestions or different methods/ways to do the same thing, please, suggest to me.

P.S.: Already spent 1 day to solve it

Answers:

Both ytNames[] and vdoID[] are arrays.

All the places you’re trying to use them, you need to give an index. Like ytNames[0] for the first one.

This is quite basic programming, you might want to brush up on arrays:
https://code.tutsplus.com/tutorials/learn-java-for-android-development-working-with-arrays–mobile-2894