我能够从YouTube API返回搜索结果。
如何显示搜索结果的持续时间?我看不到JSON数据中任何地方的持续时间。
function ySearch(e) {
let searchYt = searchTermYt.value;
search = encodeURIComponent(searchYt);
let urlYt = 'https://www.googleapis.com/youtube/v3/search/?part=snippet&type=video&key=' + apiYt + '&q=' + searchYt + '&maxResults=5';
if (e.target.token) {
urlYt += '&pageToken='+e.target.token;
}
fetch(urlYt).then(function (response) {
return response.json()
}).then(function (data) {
// check for page token to enable or disable page buttons
if(data.prevPageToken){
btnPrev.token = data.prevPageToken;
btnPrev.disabled = false;
}else{
btnPrev.token = false;
btnPrev.disabled = true;
}
if(data.nextPageToken){
btnNext.token = data.nextPageToken;
btnNext.disabled = false;
}else{
btnNext.token = false;
btnNext.disabled = true;
}
return data.items.map(function (x) {
return {
title: x.snippet.title,
// use id in URL to watch video
id: x.id.videoId
}
})
}).then(function (array) {
renderData(array);
}).catch(function (error) {
console.log(error);
})
}
搜索结果不包含视频持续时间。您必须从搜索结果中获取录像带,并再次呼叫视频:使用videoids和零件参数" contentDetails"列表。
示例:https://www.googleapis.com/youtube/v3/videos?part=contentDetails&Amp;Id=gvlsvj7bebe& keyke = api_key
将返回
{
"kind": "youtube#videoListResponse",
"etag": ""Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/CbYwR7FePSZfjlzY4mNeMODOwJA"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": ""Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/9xoE3Yy_gyWHNwLgpi4jyoRrmGY"",
"id": "gVLsVj7BebE",
"contentDetails": {
"duration": "PT4M2S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": false,
"projection": "rectangular"
}
}
]
}