Can not insert multiple videos into a playlist - y

2019-02-17 23:53发布

问题:

I am trying to add multiple videos to a playlist, but only one video is added to the playlist. I can successful create a playlist and insert a video to the playlist, but can not insert multiple videos to the playlist.

Below is a simple way that I am doing this. The function addTheseVideosToPlaylist() is specifically where I am failing. createPlaylist() and addToPlaylist() are also shown.

There is a global playlistId to keep track of created playlist.

var playlistId

I create a Playlist like this:

function createPlaylist() {
  var request = gapi.client.youtube.playlists.insert({
    part: 'snippet,status',
    resource: {
      snippet: {
        title: 'hard coded title',
        description: 'Hard Coded Description'
      },
      status: {
        privacyStatus: 'private'
      }
    }
  });

  request.execute(function(response) {
    var result = response.result;
    if (result) {
      playlistId = result.id;
      console.log("created playlist " + playlistId)
    } 
  });
}

I add a video to the created playlist given a valid video id like below :

function addToPlaylist(id, startPos, endPos) {
  console.log("in addToPlaylist with " + id  + "sending to playlist : " + playlistId);

  var details = {
    videoId: id,
    kind: 'youtube#video'
  }

  var request = gapi.client.youtube.playlistItems.insert({
    part: 'snippet',
    resource: {
      snippet: {
        playlistId: playlistId,
        resourceId: details
      }
    }
  }).execute();
}

The two above functions are fairly standard and work fine. However I have problems when adding multiple videos to a playlist like below in addTheseVideosToPlaylist(). I have an array of valid video ids and for each id, I will add it to the created playlist. The problem is that not all of the videos are added to the playlist, only one video is added.

function addTheseVideosToPlaylist() {
  var links = [
    "wtLJPvx7-ys",
    "K3meJyiYWFw",
    "3TtVsy98ces"
  ]
  for(i = 0; i < links.length; i++) 
    addToPlaylist(links[i]);
}

All in all, I am successful in creating a playlist and adding a video to the playlist, but when I try to insert multiple videos to a playlist by adding each link in an array, the playlist only contains one video.

I appreciate any help if anyone knows how to resolve this problem.

回答1:

In addition to Rohan's answer, the function call at the bottom should be:

 function myLoop(video_id) {
         addToPlaylist(video_id);
         setTimeout(function() {
             counter++;
             if(counter < links.length)
                myLoop(links[counter]);
         }, 3000);
   }

It lacked "video_id" as parameter.

Worked good for me.

The whole, working code is:

    // global array holds links and a global counter variable
  var links = [
    "wtLJPvx7-ys",
    "K3meJyiYWFw",
    "3TtVsy98ces"
  ]
  var counter = 0;

function addVideosToPlaylist() {
    myLoop(links[0]);
}


     function myLoop(video_id) {
         addToPlaylist(video_id);
         setTimeout(function() {
             counter++;
             if(counter < links.length)
                myLoop(links[counter]);
         }, 3000);
   }


回答2:

I know this an old question but I think I now understand why you need to add delay. you need to delay each insert request before you send the next one. My solution is recursion, only when I get a response from the request I'm sending the next request till the end of the array

function addVideoToPlayList(pId, videosIdArray, index)
{
      var vId = videosIdArray[index];
      var details = {
            videoId: vId,
            kind: 'youtube#video'
      }

      var request = gapi.client.youtube.playlistItems.insert({
        part: 'snippet',
        resource: {
          snippet: {
            playlistId: pId,
            resourceId: details
          }
        }
      });
      request.execute(function(response) {
          console.log(response);

          if(videosIdArray.length == index+1)
          {
              //end! 
          }
          else{
              addVideoToPlayList(pId,videosIdArray,++index);
          }

        $('#status').html($('#status').html()+'<pre>' + JSON.stringify(response.result) + '</pre><br/>');
      });
}

example of how to call this function:

addVideoToPlayList(destPlaylistId,videosIdArray,0);


回答3:

One solution is to add delays for every insert into a Playlist. I'm not entirely sure why a delay is needed though.

I am using a custom loop to with setTimeout(); Example implementation using delays:

// global array holds links and a global counter variable
  var links = [
    "wtLJPvx7-ys",
    "K3meJyiYWFw",
    "3TtVsy98ces"
  ]
  var counter = 0;

function addVideosToPlaylist() {
    myLoop(links[0]);
}


     function myLoop() {
         addToPlaylist(video_id);
         setTimeout(function() {
             counter++;
             if(counter < links.length)
                myLoop(links[counter]);
         }, 3000);
   }