【问题标题】:Getting Youtube Title given an id获取给定 ID 的 Youtube 标题
【发布时间】:2014-06-14 16:52:39
【问题描述】:

我找到了一个很好的参考来获取给定 Youtube 视频 ID 的 youtube 视频标题,但我不确定如何在他提供的函数中返回标题。在 videoInfoCallback 函数中我让它返回消息但我不确定是什么被传递到 info 参数中。有什么建议。这是我正在使用的资源:@​​987654321@

这是我正在玩的代码:

function registerScript(url) {
            var s = document.createElement('script');
            s.type = 'text/javascript';
            s.src = url;
            document.getElementsByTagName('head')[0].appendChild(s);
        }

        function videoInfoCallback(info) {
            if (info.error) {
                alert('Error\n\n' + info.error.message);
            } else {
                var message = info.data.title;
                return message;
            }
            return "hello";
        }


        function getVideoInformation(id) {
            if (id) {
                return registerScript('https://gdata.youtube.com/feeds/api/videos/' + id + '?v=2&alt=jsonc&callback=videoInfoCallback');
            } else {
                alert('Please enter an id.');
            }
        }

基本上,我想要一个函数来接收像 CFF0mV24WCY 这样的 Youtube 视频 id 并让它返回标题。

【问题讨论】:

  • 您可以在浏览器中使用调试器并在回调函数中设置断点来查看info 包含的内容。
  • 好的,我知道内容是什么,但它只是一个包含所有视频信息的对象。这是在做什么?v=2&alt=jsonc&callback=videoInfoCallback'?
  • 它们是记录在 here 的 YouTube API 的查询参数。 v 是 API 的版本,alt 是返回数据的格式,callback 是返回数据后触发的回调函数。

标签: javascript jquery html json youtube


【解决方案1】:

它并没有真正回答您的上述问题,但它可能会帮助您完成您的任务

它只是一个简单的 youtube 搜索,会带来标题视频和观看次数

html

<input type="text" id="Search"  /><br/>
<input id="show" type="button" value="Submit" onclick="Show();"/>

<div id="result">

</div>

JavaScript

$(document).ready(function () {
    $("#show").click(function () {
        getYoutube($("#Search").val() + "Offical Film Trailer");
    });
});

function getYoutube(title) {
    $.ajax({
        type: "GET",
        url: yt_url = 'http://gdata.youtube.com/feeds/api/videos?q=' + title + '&format=5&max-results=1&v=2&alt=jsonc',
        dataType: "jsonp",
        success: function (response) {
            if (response.data.items) {
                $.each(response.data.items, function (i, data) {
                    var video_id = data.id;
                    var video_title = data.title;
                    var video_viewCount = data.viewCount;
                    var video_frame = "<iframe width='600' height='385' src='http://www.youtube.com/embed/" + video_id + "' frameborder='0' type='text/html'></iframe>";
                    var final_res = "<div id='title'>" + video_title + "</div><div>" + video_frame + "</div><div id='count'>" + video_viewCount + " Views</div>";
                    $("#result").html(final_res);
                });


            } else {
                $("#result").html("<div id='no'>No Video</div>");
            }
        }
    });
}

【讨论】:

  • 谢谢!我去看看
  • 如果有帮助请告诉我
  • final is reserved keyword 不要将其用作变量名。我已将其更改为final_res
猜你喜欢
  • 1970-01-01
  • 2015-06-23
  • 2019-02-10
  • 2012-05-22
  • 2014-08-16
  • 2017-10-09
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多