2012-04-06 101 views
0

您好,我一直在使用Google Picker API(http://code.google.com/apis/picker/)過期。我有一個可以搜索YouTube電影的工作演示(代碼如下)。Google Picker API:過濾YouTube電影

當前版本返回所有視頻。我試圖過濾結果,因此它只會列出來自youtube.com的搜索結果。 picker API支持這一點。但我不明白API文檔。

該文檔(http://code.google.com/apis/picker/docs/reference.html)提到'VideoSearchView.YOUTUBE',並將其描述爲「適用於VideoSearchView.setSite()方法的字符串常量」。

我不明白如何在下面的代碼中實現此過濾器。任何幫助表示讚賞。

<!-- 
Needs work; it should only display YouTube videos. 

http://code.google.com/apis/picker/docs/reference.html 

Change the key parameter for a domain+path specific API key. Get one here: http://code.google.com/apis/loader/signup.html. 
--> 
<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAANAaPTI0Sup-knGFaDbCNHBSXhCTdTCKo5q_OHnpA1qEpBIP8mRTtPnObFFbe_J21oviL78C86yxHUA"></script> 
<script type="text/javascript"> 
    google.load('picker', '1', {'language':'nl'}); 

    function googlePicker() 
    { 
     /* 
     Displays the users own YouTube movies: 
     picker = picker.addView(google.picker.ViewId.YOUTUBE); 

     Displays all videos: 
     picker = picker.addView(google.picker.ViewId.VIDEO_SEARCH); 

     Displays all videos from youtube.com: 
     ??? 

     Example query that returns non-YouTube results: "Mobile Healing Rooms: Following Jesus on Vimeo" 
     */ 

     var picker = new google.picker.PickerBuilder(); 
     picker = picker.addView(google.picker.ViewId.VIDEO_SEARCH); 
     picker = picker.enableFeature(google.picker.Feature.NAV_HIDDEN); 

     picker = picker.setTitle('Selecteer een YouTube video'); 
     picker = picker.setCallback(googlePickerCallback); 
     picker = picker.build(); 
     picker.setVisible(true); 
    } 

    function googlePickerCallback(data) { 
     var youTubeUrl = (data.action == google.picker.Action.PICKED) ? data.docs[0].url : ''; 

     if (youTubeUrl != '') 
     { 
      $('#block_youtube_url').val(youTubeUrl); 
     } 
    } 
</script> 

回答

1

請嘗試以下等效:

// Create and render a Picker object for searching YouTube videos. 
function createPicker() { 
    var picker = new google.picker.PickerBuilder(). 
     addView(new google.picker.VideoSearchView(). 
      setSite(google.picker.VideoSearchView.YOUTUBE)). 
     setCallback(pickerCallback). 
     build(); 
    picker.setVisible(true); 
} 

如果通過ViewId添加視圖,你沒有得到一個機會來調用視圖具體方法。這就是爲什麼一些View派生類被暴露的原因。

+0

非常感謝!很好的解決方案。它甚至爲界面添加了一個YouTube徽標:) – 2012-04-20 09:27:40