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. Seinfeld(杭电3351)

    Seinfeld Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  2. 怎样使用Entityframework.Extended

    这个插件真的非常有用,我们能够使用下面语法来简化我们的工作,下面不过演示样例: Deleting <strong>//delete all users where FirstName ma ...

  3. C 标准库 - <time.h>

    C 标准库 - <time.h> 简介 time.h 头文件定义了四个变量类型.两个宏和各种操作日期和时间的函数. 库变量 下面是头文件 time.h 中定义的变量类型: 序号 变量 &a ...

  4. 关于 AlphaGo 论文的阅读笔记

    这是Deepmind 公司在2016年1月28日Nature 杂志发表论文 <Mastering the game of Go with deep neural networks and tre ...

  5. Flex4_Tree组件1(添加、删除、展开、关闭、右键菜单)

    1.屏蔽系统菜单:工程目录“html-template”文件夹-->“index.template.html”文件中,在var params = {};语句下添加新语句:        para ...

  6. C#应该掌握的一些东西

    C#应该掌握的一些东西   随着培训机构的增多,越来越多的人进入IT行业.那么对于我们这些自学出来,经验不够丰富的转行者来说,我们需要掌握最起码的一些东西,这对于面试很有用,而且在工作中也很常用.本人 ...

  7. 第14章8节《MonkeyRunner源代码剖析》 HierarchyViewer实现原理-获取控件列表并建立控件树

    在上几节的描写叙述中,我们把HierarchyViewer初始化好.也把ViewServer给装备好了.那如今距离获得一个控件去操作它是万事具备仅仅欠东风了,欠了那一股春风了?欠了的是建立控件树这个东 ...

  8. Intel平台map

  9. adb tcp 调试

    su setprop service.adb.tcp.port 5555 stop adbd start adbd

  10. iOS 7 中 StoryBoard 总体缩放

    iOS 7 中 StoryBoard 总体缩放 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用 ...