本文介绍了无法将多个视频插入播放列表 - YouTube API v3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个视频添加到播放列表中,但只有一个视频添加到播放列表中.我可以成功创建播放列表并将视频插入播放列表,但无法将多个视频插入播放列表.

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.

下面是我这样做的一种简单方法.函数 addThisVideosToPlaylist() 特别是我失败的地方.还显示了 createPlaylist() 和 addToPlaylist().

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.

有一个全局播放列表 ID 来跟踪创建的播放列表.

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)
        }
    });
}

我将一个视频添加到创建的播放列表中,并给出如下所示的有效视频 ID:

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();
}

以上两个函数都相当标准并且工作正常.但是,在将多个视频添加到如下所示的 addThisVideosToPlaylist() 中的播放列表时,我遇到了问题.我有一组有效的视频 ID,对于每个 ID,我会将其添加到创建的播放列表中.问题是不是所有的视频都添加到播放列表中,只添加了一个视频.

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.

我该如何解决这个问题?

How can I resolve this problem?

推荐答案

除了罗汉的回答,最下面的函数调用应该是:

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);
}

它缺少video_id"作为参数.

It lacked "video_id" as a parameter.

它对我很有用.

整个工作代码是:

// 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);
}

这篇关于无法将多个视频插入播放列表 - YouTube API v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 13:44