youtube iframe api getVideoData被删除,如何获得标题?

时间:2017-11-14 09:37:39

标签: youtube-api youtube-iframe-api

在13月11日,我接到了一位客户的电话,其中youtube播放器不再工作,在快速查看开发工具后,出现了错误

Uncaught TypeError: a.getVideoData is not a function

快速查看后,在Player对象所包含的内容中,getVideoData的no函数。

GetVideoData是获取视频标题的一种方式,但我现在如何获得标题?谷歌有没有关于这些变化的文章?

3 个答案:

答案 0 :(得分:3)

要获取视频标题,您可以查询YouTube Data API v3

<center><p id="demo" style="color:black;"></p></center>

<script>

window.onload = function() {
  typeWriter();
};
var i =0;
var txt = "Would it be ok if I wrote you a rhyme? would it be ok if I opened my heart? Would it be ok if I took on the part Of being your man and showed you a view, One that only a real man could do? Would it be ok if I could make you smile? Would it be alright to look in your eyes? Would it be alright to never tell lies? Would it be alright to find a way? Would it be alright to long for the day To pull you close and whisper in your ear And tell you our feelings are nothing to fear? Would it be ok if I took some of your time? Would it be ok if I wrote you a rhyme? To tell you there is nothing I would rather do Than spend my whole life loving only you... ";
var txt1= '';
var speed = 100;
function typeWriter() {
  if (i < txt.length) {
    document.getElementById("demo").innerHTML += txt.charAt(i);
document.getElementById("demo").innerHTML += txt1.charAt(i);

    i++;
      setTimeout(typeWriter, speed);

  }

}
</script>

为此,您需要注册Google Cloud Console并创建一个API密钥(它是免费的)。您可以将API密钥限制为仅在您的网站上使用,这样您就可以安全地将其公开在JS源代码/ html代码中,而其他人无法代表您进行查询。确保在控制台中启用YouTube Data API v3,否则您的查询将返回错误。

上述查询将返回您感兴趣的视频信息的JSON表示(GET https://www.googleapis.com/youtube/v3/videos ?part=snippet &id=VIDEO_ID &key=YOUR_API_KEY 部分)。假设您将JSON解析为名为snippet的对象。然后你可以通过

获得视频标题
result

答案 1 :(得分:2)

getVideoData()似乎又回来了(2017年12月)。所以,再试一次!

答案 2 :(得分:1)

截至今天(2020年10月1日),我正在从YouTube的API对象中检索视频的标题:

// Assigning YouTube's ID to your ID variable
const playerID = "xxxxxxx";

// Creating an object for the video using YouTube's API.
const yPlayer = new YT.Player(playerID, {
    events: {
        'onReady': onPlayerReady(),
        'onStateChange': onPlayerStateChange()
    }
});

function onPlayerReady() {
}

function onPlayerStateChange() {
    // Title retrieved here
    let videoTitle = yPlayer.j.videoData.title;
}

onYouTubeIframeAPIReady();