gpt4 book ai didi

youtube - 从 as3 中的 youtube 播放列表中获取视频 ID

转载 作者:行者123 更新时间:2023-12-03 05:59:19 28 4
gpt4 key购买 nike

如何从 as3 中的 youtube 播放列表中获取视频 ID,以便从您的 Tube 播放列表中播放最新上传的视频。

我已经加载播放列表并且它可以正常播放,但我只想播放播放列表中的最新视频。

谢谢

最佳答案

您可以使用 Youtube Data API获取有关播放列表中项目的信息。

为了能够使用 API,您需要生成 API key 。 Info about how to do so can be found here .

要获取某个播放列表中的最新视频,您可以对该 URL 发出 GET 请求:

https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=1&playlistId={YOUR_PLAYLIST_ID}&key={YOUR_API_KEY}

Let's break it down to see what it does:

  • https://www.googleapis.com/youtube/v3/playlistItems - This is the URL endpoint to request data about playlist items in the Youtube Data API
  • ?part=contentDetails - We define what details we want returned from the query. In this case we want the video id's, so we use contentDetails
  • &maxResults=1 - We are only interested in one item - the latest. (It appears that the result is ordered from latest to oldest, so by only requesting the first video it should return the latest.)
  • &playlistId={YOUR_PLAYLIST_ID} - Replace this value with the ID of the playlist in question.
  • &key={YOUR_API_KEY} - Replace this value with your API key.

Let's look at a sample request:

var playlistUrl:String = "https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=1&playlistId=PL4Sz7_l-PtwAG8xye4Y9whykc4OAwbN9f&key={YOUR_API_KEY}"
var playlistReq:URLRequest = new URLRequest(playlistUrl);
var playlistLoader:URLLoader = new URLLoader();
playlistLoader.addEventListener(Event.COMPLETE, handlePlaylistUrlLoaded);
playlistLoader.load(playlistReq);

function handlePlaylistUrlLoaded(e:Event):void {
trace(playlistLoader.data); // See the response below
var playlistResponse:Object = JSON.parse(playlistLoader.data); // Parse the JSON response
trace(playlistResponse.items[0].contentDetails.videoId); // Prints "21Ox58OKFmo" which is the video id of the latest video in this playlist
}

对此示例查询的原始响应是:
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"FOuwADrXJjsTKgUIQJoQC6nKNFY/n3kL7MtTJyPOfHU6jmro9gAC1CI\"",
"nextPageToken": "CAEQAA",
"pageInfo": {
"totalResults": 189,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#playlistItem",
"etag": "\"FOuwADrXJjsTKgUIQJoQC6nKNFY/aNpryIHyioAUu4C9_WVP8kZGbuE\"",
"id": "PL88DYY_1F2muJ4Dv0iAFHEuKQuma2y_BH0WbF7AzESJw",
"contentDetails": {
"videoId": "21Ox58OKFmo"
}
}
]
}

编辑以响应更多细节

如果您想使用 YouTube 播放器 API,在将播放列表添加到提示后,您似乎必须等待“视频提示”事件。

因此,从本质上讲,这应该有效(未经测试的代码):
youtubePlayer.addEventListener("onStateChange", onPlayerStateChange);
youtubePlayer.cuePlaylist("{YOUTUBE_PLAYLIST_ID}");

function onPlayerStateChange(event:Event):void {
if (Object(event).data == YT.PlayerState.CUED) {
// Playlist cued, the playlist data should now be accessible
var playlist:Array = youtubePlayer.getPlaylist();
trace(playlist[0]); // This value should now exist
}
}

关于youtube - 从 as3 中的 youtube 播放列表中获取视频 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24541079/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com