3

I'm new to this website and so far it really helped me just from looking. I'm building and app that contains a playlist from youtube, so far it's working but I only have one video and the next video will only show if you press on the next button, I want to create a list of all videos on that playlist. Currently using java for android and xml, would like to keep it that way if it's possible. Thank you.

my code:

import android.drm.DrmStore;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.ErrorReason;
import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.OnFullscreenListener;

public class ky extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
    public static final String API_KEY = "api";
    //http://youtu.be/<VIDEO_ID>
    public static final String VIDEO_ID = "PLZeI1YOvEK7EK499BrkBl95VseODbMDa-";

    public void onFullscreen(boolean isFullscreen) {
        if (isFullscreen)
            playbackEventListener.onPlaying();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /** attaching layout xml **/
        setContentView(R.layout.ky);

        /** Initializing YouTube player view **/
        YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
        youTubePlayerView.initialize(API_KEY, this);
    }

    @Override
    public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
        Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onInitializationSuccess(Provider provider, final YouTubePlayer player, boolean wasRestored ) {
        /** add listeners to YouTubePlayer instance **/
        player.setPlayerStateChangeListener(playerStateChangeListener);
        player.setPlaybackEventListener(playbackEventListener);
        player.setOnFullscreenListener(new OnFullscreenListener() {
            @Override
            public void onFullscreen(boolean b) {
                if(b)
                    player.play();
                else
                    player.play();
            }
        });


        /** Start buffering **/
        if (!wasRestored) {

            player.loadPlaylist(VIDEO_ID);


        }
    }


    private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {



        @Override
        public void onBuffering(boolean arg0) {
        }

        @Override
        public void onPaused() {
        }

        @Override
        public void onPlaying() {
        }

        @Override
        public void onSeekTo(int arg0) {
        }

        @Override
        public void onStopped() {
        }

    };


    private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {

        @Override
        public void onAdStarted() {
        }

        @Override
        public void onError(ErrorReason arg0) {
        }

        @Override
        public void onLoaded(String arg0) {
        }

        @Override
        public void onLoading() {
        }


        @Override
        public void onVideoEnded() {
        }

        @Override
        public void onVideoStarted() {
        }
    };
}

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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:background="@color/black"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".example">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtube_player"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"/>

</RelativeLayout>
Orel Vadana
  • 31
  • 1
  • 3
  • 1
    Welcome! You'll want to edit your post to show what you've tried, and then either the bad output and/or errors you're getting. I'm not going to downvote your question until you have a chance to edit, but don't be shocked if others do. You'll want to read [this](http://stackoverflow.com/help/how-to-ask) before you edit. – LDMJoe Nov 19 '15 at 15:07

4 Answers4

5

I've done this using Youtube Api rest calls, async task and a recyclerview.

RecyclerView helps create a list and async task helps you make http requests in the background.

Look up the basics of async task and recyclerview. http://www.androidhive.info/ has great tutorials on this.

The Youtube rest call will look like this(I believe the max results are 50):

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=iBi9LVIrC-fVelw2-I2r-yrEk6SpXfO8&key=AIzaSyCI1oCTXwZzgVv7LDQ8NykSIUEWt247KnU&maxResults=50

Here are some steps to get you started:

For example in this playlist url: https://www.youtube.com/watch?v=2q7XGuH2Q-s&list=PLuR1PJnGR-Igj4mNcr6_zDkQ3DTVA6wG5

Everything after "PL" is the id "uR1PJnGR-Igj4mNcr6_zDkQ3DTVA6wG5".

Also now you have to create a browser key in Google Developer console. You don't have to enter anything in the textbox, just click enter, and retrieve your api key

In your Google Developers console,under "APIs" make sure you turn Youtube Data api.

https://www.dropbox.com/s/6wtr7hpawx3ij64/youtubeApi.PNG

https://developers.google.com/youtube/android/player/downloads/

Add the jar file from the downloads to your libs folder in your newly downloaded project.DONT add it by dragging it into Android Studio

Next go to android studio and click the project drop down:

https://www.dropbox.com/s/cjlr4fnnikpnx7a/Screen%20Shot%202015-04-22%20at%2012.39.28%20PM.png?dl=0

then drop down the libs folder and you will see the youtube jar.right click the jar file and click “add as library”.

I have a more in-depth tutorial here: http://cmcoffee91.com/blog/2016/12/18/how-to-add-youtube-playlist-to-your-android-app/

cmcoffee91
  • 141
  • 3
  • 8
0

You can get everything with youtube data api, it's very easy and simple.

https://developers.google.com/youtube/v3/

vivek
  • 284
  • 3
  • 8
0

visit youtube developer api website. There are several methods in the api which can help to retrieve playlists,channels,users profile etc.For reference to insert playlist

Kartik Ohri
  • 325
  • 6
  • 19
0

The other two answers are correct. Specifically, the YouTube API REST call you are looking for is under PlaylistItems, which gets a list of all videos in a playlist.

From the docs, the GET call is:

GET https://www.googleapis.com/youtube/v3/playlistItems?part=id&playlistId=[yourPlaylistId]
Captain Delano
  • 427
  • 1
  • 4
  • 12
  • I've seen this already but I have no idea how does this thing works. This should be on the java page? – Orel Vadana Nov 19 '15 at 19:29
  • Java has [plenty of tools](http://stackoverflow.com/questions/3913502/restful-call-in-java) to make HTTP requests to REST endpoints like the above. Google around for lots of great examples. – Captain Delano Nov 20 '15 at 06:16
  • Do I need some jar files other than youtube jar file? I'm getting an invalid token error when writimg the GET line. – Orel Vadana Nov 20 '15 at 11:13
  • I would recommend you look up some tutorials on how to make GET requests or HTTP calls in Java. There are lots of good resources on this already. The above information will be helpful to you after you figure out how to make a GET request. – Captain Delano Nov 20 '15 at 20:07