本文是由外部開發人員所撰寫及提交。 YouTube API 和工具團隊感謝 Martin Legris 貢獻寶貴的時間與專業知識。
Martin Legris
2008 年 3 月
簡介
在本教學課程中,我將示範如何透過不到 10 行實際的 AS3 程式碼,擷取 YouTube 上過去 day
、week
、month
或 all_time
最熱門的影片。您可以取得下列任一標準動態饋給:
- 觀看次數最多
- 由新到舊
- 最多留言的影片
- 最多連結
- 最多影片回應
- 最新精選影片
- 評分最高
- 最受喜愛影片
- 行動裝置適用的影片
本例中使用的程式庫是硬式型別,也就是說,收到資料後,您可以在 FlashDevelop、Flex IDE、Eclipse w/FDT、IntelliJ Idea 等 IDE 中使用自動完成功能。
重要資源
開始之前,我們先列出可供您 (有意使用 YouTube API 資料的 AS3 開發人員) 使用的資源。
- 我開發的 AS3 程式庫,用於使用這些網路服務
- 官方 YouTube Data API 說明文件
- FlashDevelop IDE (適用於 AS3)
- 在 YouTubeClient 上完成哪些要求呼叫時,觸發哪些事件的清單
深入瞭解
提出第一個要求
以下程式碼的註解相當完整。不過,我會稍微說明功能後再深入探究。所有要求的起點都是名為 YouTubeClient
的類別。這個類別是單例模式,您可以透過以下方式存取:
// first you import it import ca.newcommerce.youtube.webservice.YouTubeClient; // then you get a reference to it var client:YouTubeClient = YouTubeClient.getInstance();
我們現在可以提出要求了:
client.getStandardFeed(YouTubeClient.STD_TOP_RATED, YouTubeClient.TIME_MONTH, 1, 10);
我剛剛要求取得上個月的熱門影片,結果 1 到 10 (請注意,每個呼叫最多可取得 50 個結果)。就是這麼簡單!
處理結果
那麼我要如何存取這些結果?由於 REST 類型網路服務架構為非同步,因此使用事件處理結果會比較容易,而不會在擷取結果時停止程式碼。在此情況下,我們需要為 StandardVideoFeedEvent.STANDARD_VIDEO_DATA_RECEIVED
事件宣告事件監聽器。每次 YouTubeClient
收到標準動態饋給的回應時,系統就會呼叫我們的函式。
client.addEventListener(StandardVideoFeedEvent.STANDARD_VIDEO_DATA_RECEIVED, doVideosReceived);
要呼叫的函式為 doVideosReceived
。這個函式會採用一個參數,也就是 StandardVideoFeedEvent
類型的變數。這項做法符合 AS3 標準。讓我們宣告一下,並讓它追蹤一些與我們收到的影片相關的資訊。我們會追蹤至標準輸出內容:
- 影片標題,
- 要嵌入影片的 SWF 網址。
- 觀看次數
- 留言數,
- 影片長度
- 以及作者姓名
function doVideosReceived(evt:StandardVideoFeedEvent):void { // get a reference to the feed containing the videos var feed:VideoFeed = evt.feed; // variable to hold each video retreived on the feed var video:VideoData; // iterate through the availabe results while(video = feed.next()) { // trace a newline followed by the video title trace("\nvideo title:"+video.title); // trace the url to use to embed the flash player with this video playing in it.. trace("player url:"+video.swfUrl); // the view count trace("viewCount:"+video.viewCount); // the comment count trace("commentCount:"+video.commentCount); // the duration trace("duration:"+video.duration); // the author trace("author:"+video.authors.first().name); } }
關於媒體庫和動態饋給的注意事項
幾乎所有事件都有 .feed
屬性。由於此呼叫只傳回一筆記錄,因此 ProfileEvent
本身就是 .profile
屬性。
動態饋給的類型有很多,全都支援以下方法:
first()
-- 擷取第一筆記錄並指向該記錄next()
:擷取下一個可用的記錄last()
-- 擷取上一筆記錄並指向該記錄previous()
-- 擷取動態饋給中的先前記錄getAt()
:取得特定位置的結果count()
:這個動態饋給可用的結果數量,與totalResults
不同
當您到達結果的結尾時,next()
會傳回 null。同樣地,一旦您取得第一個結果,下一次對 previous()
的呼叫就會傳回 null
。
請注意,動態饋給中的資料 (例如類別) 會包裝在 Iterators
中,因此您可以使用相同的函式瀏覽可用的類別。
完整原始碼
以下是這個範例的完整原始碼。您可以下載 ZIP 檔案,其中包含在 Flash CS3 中執行範例所需的所有內容,但最好還是更新程式庫,以防我們修正錯誤或更新功能。如要下載程式庫,請按這裡。
package { // first import dependencies (You can be more specific than this if you want) import ca.newcommerce.youtube.data.*; import ca.newcommerce.youtube.events.*; import ca.newcommerce.youtube.feeds.*; import ca.newcommerce.youtube.iterators.*; import ca.newcommerce.youtube.webservice.YouTubeClient; public class ytTest() { // some class variables protected var _ws:YouTubeClient; protected var _requestId:Number; public function ytTest() { // now inside of an init function _ws = YouTubeClient.getInstance(); // register to list to the events you are interested in _ws.addEventListener(StandardVideoFeedEvent.STANDARD_VIDEO_DATA_RECEIVED, doVideosReceived); // do your call.. get the Top Rated videos for the last month // results 1 to 10; it returns a requestId _requestId = _ws.getStandardFeed(YouTubeClient.STD_TOP_RATED, YouTubeClient.TIME_MONTH, 1, 10); } protected function doVideosReceived(evt:StandardVideoFeedEvent):void { // get a reference to the feed containing the videos var feed:VideoFeed = evt.feed; // variable to hold each video retrieved on the feed var video:VideoData; while(video = feed.next()) { // trace a newline followed by the video title trace("\nvideo title:"+video.title); // trace the swf URL (used for embedding) trace("player url:"+video.swfUrl); // the view count trace("viewCount:"+video.viewCount); // the comment count trace("commentCount:"+video.commentCount); // the duration trace("duration:"+video.duration); // the author trace("author:"+video.authors.first().name); } } } }
結論
雖然本文僅觸及冰山一角,但會讓您瞭解使用這個程式庫查詢 YouTube Data API 有多簡單。由於我已花時間將所有內容輸入,因此很容易就能瞭解每個動態饋給回應可提供哪些資料。我們尚未支援新的寫入和上傳功能,但如果您想為程式庫貢獻內容,請前往專案頁面。
作者簡介

Martin Legris 擁有 12 年的軟體開發經驗。目前,他主要專注於使用者介面研究,最喜歡的工具是 Flash 與 ActionScript。歡迎前往 blog.martinlegris.com 瀏覽他的網誌,以及前往 www.newcommerce.ca 瀏覽他的網站 (可能已過時)。
這項內容採用的授權為 Creative Commons 姓名標示 3.0 美國授權。