如何从as3播放列表中获取视频ID,以便播放管播放列表中的最新上传视频。
我已加载pLay列表并且播放正常但我想只播放播放列表中的最新视频。
谢谢
答案 0 :(得分:1)
您可以使用Youtube Data API获取有关播放列表中项目的信息。
为了能够使用API,您需要生成API密钥。 Info about how to do so can be found here
要获取某个播放列表中的最新视频,您可以对此网址发出GET请求:
https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=1&playlistId={YOUR_PLAYLIST_ID}&key={YOUR_API_KEY}
让我们分解它看看它的作用:
contentDetails
让我们看一个示例请求:
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
}
}