YoutubeAPI使用

1  Youtube API能干什么

2  Youtube API

2.0 Youtube简介

2.1 如何使用Youtube API

2.1.1 获取Youtube 的开发者ID

2.1.2 Youtube API使用

2.3 如何播放检索到的video

2.3.1 使用http请求播放

2.3.2 将播放窗体嵌入到自己的页面内播放

3  Sample实现

1  Youtube API能干什么

服务网站:http://www.youtube.com/

l         Youtube API支持搜索用户信息和video信息, 支持多种搜索适用。

l         能很方便的在APP中播放检索到的短片; 只能在播放前设置播放窗口的大小, 但不能实现各种播放模式。

l         提供sample。

相关资源:

http://www.youtube.com/dev                                                  Youtube 开发

http://www.youtube.com/dev_docs                                          Youtube API 文档

http://www.youtube.com/dev_intro                            使用Youtube API起点

2  Youtube API

2.0 Youtube简介

YouTube是一个因特网网站,让使用者上载观看及分享短片。它是一个可供网民上载观看及分享短片的网站,至今已成为同类型网站的翘楚,并造就多位网上名人和激发网上创作。

2.1 如何使用Youtube API

使用Youtube API首先要申请一个开发者ID(该ID会作为API参数)。API的调用是一个http请求,返回的是一个xml结果集。

2.1.1 获取Youtube 的开发者ID

首先需要申请一个Youtube 帐户, 然后设置developer profile, 设置成功后会产生developer ID。 我申请到的ID是:Gy-vvp-8HnE, 如下:

Developer ID:

Gy-vvp-8HnE

Purpose for Using APIs:

 

Site Name:

Site URL:
(optional)

Secret:
(for read/write API calls)

Platform(s):
(optional, check all that apply)

C#
Python
Java
PHP
Ruby
Perl
ColdFusion

2.1.2 Youtube API使用

Youtube API功能如下表:

User Information

Video Viewing

 

由于这些API使用类似,这里举两个例子,详细使用见:http://www.youtube.com/dev_docs

youtube.users.list_favorite_videos函数说明

描述:通过输入用户名(Yubube的注册用户名),检索出该用户的喜爱的video

参数:

User:Yubube的注册用户名

调用举例:

http://www.youtube.com/api2_rest?method=youtube.users.list_favorite_videos&dev_id=Gy-vvp-8HnE&user=GiR2007

返回结果中共显示了10条机记录:这里只列举出一条信息。

<?xml version="1.0" encoding="utf-8"?>
<ut_response status="ok">
<video_list>
<total></total>
<video>
<author>j1j8</author>
<id>_pbHmupg-4M</id>
<title>Free Hugs Campaign Aberdeen</title>
<length_seconds></length_seconds>
<rating_avg>4.71</rating_avg>
<rating_count></rating_count>
<description>http://FreeHugsScotland.bebo.com Give a FREE HUG!!!! thats all i can say!!! If you spotted me please post it as a video response!</description>
<view_count></view_count>
<upload_time></upload_time>
<comment_count></comment_count>
<tags>Aberdeen Aberdeenshire free hugs hug guy peace smile happy love John gives city sign hugging sharing friendship human</tags>
<url>http://www.youtube.com/?v=_pbHmupg-4M</url>
<thumbnail_url>http://sjl-static2.sjl.youtube.com/vi/_pbHmupg-4M/2.jpg</thumbnail_url>
</video>
<video>
...
</video>
...
</video_list>
</ut_response>

youtube.videos.list_by_tag (with paging) 函数说明

描述:通过输入的tag, 检索出所有的video;

参数:

l         Method:youtube.videos.list_by_tag

l         Dev_id:申请的developer ID,即Gy-vvp-8HnE

l         Tag:检索的关键字

l         Page:如果按页显示,取第几页数据。 可选项参数

l         Per_page:一页显示记录的条数。 可选项参数

调用举例:

http://www.youtube.com/api2_rest?method=youtube.videos.list_by_tag&dev_id=Gy-vvp-8HnE&tag=sport&page=1&per_page=3

返回结果:显示了3条记录(2、3条记录略),其中粗体的信息是比较重要的。

<?xml version="1.0" encoding="utf-8" ?>
<ut_response status="ok">
<video_list>
<total></total>
<video>
<author>MollywoodTO</author>
<id>b06exGCPThQ</id>
<title>THE GAYEST SPORT ON EARTH</title>
<length_seconds></length_seconds>
<rating_avg>4.25</rating_avg>
<rating_count></rating_count>
<description>Spandex Alert-The gayest sport on earth Hard-bodied men strut their stuff but in an oh, so gay, way. (and I mean that in a good way! lol)</description>
<view_count></view_count>
<upload_time></upload_time>
<comment_count>None</comment_count>
<tags>sport men sexy spandex athletic competition mollywoodto</tags>
<url>http://www.youtube.com/?v=b06exGCPThQ</url>
<thumbnail_url>http://sjc-static5.sjc.youtube.com/vi/b06exGCPThQ/2.jpg</thumbnail_url>
</video>
<video>

</video>
<video>

</video>
</video_list>
</ut_response>

youtube.videos.get_details 函数说明

描述:获取video的详细信息;

参数:

l         Method:youtube.videos.get_details

l         Dev_ID:申请的developer ID,即Gy-vvp-8HnE

l         Video_ID:Video的ID,在前一个检索结果中有, 即 b06exGCPThQ。

调用举例:

http://www.youtube.com/api2_rest?method=youtube.videos.get_details&dev_id=Gy-vvp-8HnE&video_id=b06exGCPThQ

返回结果:

<?xml version="1.0" encoding="utf-8"?>
<ut_response status="ok">
<video_details>
<author>Sander79</author>
<title>Lost &amp; Found</title>
<rating_avg>4.56</rating_avg>
<rating_count></rating_count>
<tags>stopmotion stop-motion stop motion animation clay claymation</tags>
<description>Some stopmotion animations I lost over the years. I'm glad that I one day made a copy. Finding the copy was a lot more difficult : ) Enjoy!</description>
<update_time></update_time>
<view_count></view_count>
<comment_count></comment_count>
<upload_time></upload_time>
<length_seconds></length_seconds>
<recording_date />
<recording_location />
<recording_country />
<comment_list>
<comment>
<author>joelsheldon</author>
<text>This is immense</text>
<time></time>
</comment>
<comment>
...
</comment>
...
</comment_list>
<channel_list>
<channel>Film &amp; Animation</channel>
</channel_list>
<thumbnail_url>http://sjl-static1.sjl.youtube.com/vi/ss_619Vlyzs/2.jpg</thumbnail_url>
<embed_status>ok</embed_status>
</video_details>
</ut_response>

2.3 如何播放检索到的video

2.3.1 使用http请求播放

请求URL:http://www.youtube.com/watch?v=ss_619Vlyzs

参数v:Videoid,可以通过搜索得到。

返回的是一个网页,其中包含了包放窗体,以及该video的相关信息。

2.3.2 将播放窗体嵌入到自己的页面内播放

如果想要将播放窗体嵌入到自己的页面内需要在html中加入如下代码:

<object>
<param name="movie" value="http://www.youtube.com/v/ss_619Vlyzs"></param>
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/ss_619Vlyzs" type="application/x-shockwave-flash" wmode="transparent" width="" height=""></embed>
</object>

下面是一个完整的播放例子,将其保存为html文档即可运行。

<html>
<head>
</head>
<body>
<object>
<param name="movie" value="http://www.youtube.com/v/ss_619Vlyzs"></param>
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/ss_619Vlyzs" type="application/x-shockwave-flash" wmode="transparent" width="" height=""></embed>
</object>
</body>
</html>

说明:由于我网速太慢,在测试中能播放的时候很少(我只播放成功三次),多数情况是显示的Loading状态,见下图:

3  Sample实现

例子说明: 通过Youtube的搜索API获取video信息。在从信息中得到video的id,使用此ID就能播放该媒体。我们不需关心媒体的格式。

由于播放控件是嵌入到页面中的,而且没有提供多种播放模式, 我们只能改变播放窗口的大小。 我们只能在播放前将设置播放窗口的大小,播放过程中无法改变其大小。

Sample提供两种收缩功能:

l         用户的喜爱的video搜索,即youtube.users.list_favorite_videos

l         关键字搜索,即youtube.videos.list_by_tag

操作说明:

l         01 选择检索模式

l         02 输入检索条件;电击SearchButton

l         03 选择播放媒体播放

l         04 播放该媒体

YoutubeAPI使用的更多相关文章

  1. iOS网络高级编程:iPhone和iPad的企业应用开发之错误处理

    本章内容 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWluZ2h1YXdlbmthbmc=/font/5a6L5L2T/fontsize/400/fi ...

  2. crontab中的%

    crontab中的%是换行的意思,在使用时需要使用\做转义. ----------------- 在用crontab执行一段定时任务时,想要把数据输出到一个日期命名的文件中 * * * * * cd ...

随机推荐

  1. 微信小程序日期定位弹出框遮挡问题

    只需要用bindtap绑定一个点击后的操作(隐藏键盘): wx.hideKeyboard()

  2. HDU 4514并查集判环+最长路

    点击打开链接 题意:中文题...... 思路:先推断是否能成环,之前以为是有向图,就用了spfa推断,果断过不了自己出的例子,发现是无向图.并查集把,两个点有公共的父节点,那就是成环了,之后便是求最长 ...

  3. react 自定义 TabBar 组件

    1.创建 组件 src/components/TabBar/index.js /** * TabBar 组件 */ import React ,{ PureComponent } from 'reac ...

  4. AngularJs + html 5 实现 裁剪上传

    直接上代码 directive.js app.directive('fileUploadersm', function () { return { restrict: 'E', transclude: ...

  5. 通过Pojo对象 field 属性加注解实现格式校验,极大的降低代码量

    近期做一个接口.接受外系统的报文,通过XStream转换成java对象以后.须要对当中的字段做格式校验. 要求例如以下: 传统的方式是硬编码校验.可是对于field非常多的情况.代码量暴增.easy出 ...

  6. 03 http+socket编程批量发帖

    <?php // http请求类的接口 interface Proto { // 连接url function conn($url); //发送get查询 function get(); // ...

  7. disabled和readonly

    项目中,有一个input控件,input的值需要通过点击一个javascript链接,从弹出的对话框中所列出的项中选择.而不能从input框中直接输入. 刚开始将input的disabled属性设置为 ...

  8. EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体服务器,EasyPlayer手机播放器

    在不断进行EasyDarwin开源流媒体服务器的功能和性能完善的同时,我们也配套实现了目前在安防和移动互联网行业比较火热的移动端手机直播方案,主要就是我们的 EasyPusher直播推送项目 和 Ea ...

  9. 九度OJ 1126:打印极值点下标 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4613 解决:1646 题目描述: 在一个整数数组上,对于下标为i的整数,如果它大于所有它相邻的整数, 或者小于所有它相邻的整数,则称为该整 ...

  10. 九度OJ 1109:连通图 (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2783 解决:1432 题目描述: 给定一个无向图和其中的所有边,判断这个图是否所有顶点都是连通的. 输入: 每组数据的第一行是两个整数 n ...