2016-03-30 18 views
1

関連動画をサムネイルとして特定の動画に取り込む際に問題が発生します。私のコードでは、search->listを正しく設定し、関連する動画のさまざまなタイトルとサムネイルURLを返します。しかし、search->listはパラメータcontentDetailsまたはstatisticsを持っていないので、正確に同じ問題hereに関連する別の質問を見つけて、videos->listを取得するためにセカンダリコールを使用しました。しかし問題は、両方の値(vidDuration & viewCount)は何も渡されておらず、項目が通過して停止すると未定義とマークされていることです。アイテムのdurationviewCountの値をsearch->listのアイテムに基づいてどのようにすることができますか?関連コンテンツの動画コンテンツの詳細と統計 - Youtube API V3

script.js:

function relatedVids(videoId) 
{ 
    debugger; 
    $.get(// get related videos related to videoId - relatedToVideoId 
     "https://www.googleapis.com/youtube/v3/search", 
     { 
      part: 'snippet', 
      maxResults: vidResults, 
      relatedToVideoId: videoId, // $.cookie("id"); from single-video.html 
      type: 'video', 
      key: 'XXXXXXXXX' 
     }, 

     function(data) 
     { 
      $.each(data.items, 
       function(i, item) 
       { 
        console.log(item); 
        var vidTitle = item.snippet.title; // video title 
        //var videoId = item.snippet.resourceId.videoId; // video id 
        //var vidDuration = item.contentDetails.duration; 
        //var viewCount = item.statistics.viewCount; 
        var vidThumbUrl = item.snippet.thumbnails.default.url; // video thumbnail url 
        var extractVideoId = null; // var to extract video id string from vidThumbUrl 

        // check if vidThumbUrl is not null, empty string, or undefined 
        if(vidThumbUrl) 
        { 
         //console.log("vidThumbUrl: ", vidThumbUrl); 
         var split = vidThumbUrl.split("/"); // split string when '/' seen 
         extractVideoId = split[4]; // retrieve the fourth index on the fourth '/' 
         //console.log("extractVideoId: ", extractVideoId); // YE7VzlLtp-4 
         //console.log("split array: ", split); 
        } 
        else console.error("vidThumbUrl is either undefined or null or empty string."); 
        // if video title is longer than 25 characters, insert the three-dotted ellipse 
        if(vidTitle.length > 25) 
        { 
         var strNewVidTitle = vidTitle.substr(0, 25) + "..."; 
         vidTitle = strNewVidTitle; 
        } 

        // search->list only takes snippet, so to get duration and view count; have to call this function that has the recognized param structure 
        $.get(
         "https://www.googleapis.com/youtube/v3/videos", 
         { 
          part: 'contentDetails, statistics', 
          id: extractVideoId, //item.snippet.resourceId.videoId, 
          key: 'XXXXXXXXX', 
         }, 
         // return search->list item's duration and view count 
         function(item) 
         { 
          vidDuration = item.contentDetails.duration; // pass item's duration 
          return vidDuration; 
         }, 
         function(item) 
         { 
          viewCount = item.statistics.viewCount; // pass item's view count 
          return viewCount; 
         } 
        ); 


        try 
        { 
         var vidThumbnail = '<div class="video-thumbnail"><a class="thumb-link" href="single-video.html"><div class="video-overlay"><img src="imgs/video-play-button.png"/></div><img src="' + vidThumbUrl + '" alt="No Image Available." style="width:204px;height:128px"/></a><p><a class="thumb-link" href="single-video.html">' + vidTitle + '</a><br/>' + convert_time(vidDuration) + '/Views: ' + viewCount + '</p></div>'; 

         // print results 
         $('.thumb-related').append(vidThumbnail); 
        } 
        catch(err) 
        { 
         console.error(err.message); // log error but continue operation  
        } 
       } 
      ); 
     } 
    ); 
} 

シングルvideo.htmlスクリプト:

$(document).ready(function() 
{ 
    var videoId; 
    $(function() 
    { 
     if($.cookie("title") != null && $.cookie("id") != null) 
     { 
      var data = '<div class="video"><iframe height="' + vidHeight + '" width="' + vidWidth + '" src=\"//www.youtube.com/embed/' + $.cookie("id") + '\"></iframe></div><div class="title">' + $.cookie("title") + '</div><div class="desc">' + $.cookie("desc") + '</div><div class="duration">Length: ' + $.cookie("dur") + '</div><div class="stats">View Count: ' + $.cookie("views") + '</div>'; 

      $("#video-display").html(data); 
      $("#tab1").html($.cookie("desc")); 

      videoId = $.cookie("id"); 
      //vidDuration = $.cookie("dur"); works but prints out the same value for each 
      //viewCount = $.cookie("views"); works but prints out the same value for each 
      debugger; 
      relatedVids(videoId); // called here 


      console.log("from single-vid.html globalPlaylistId: ", $.cookie("playlistId")); 


      // remove cookies after transferring it to this page for display 
      $.removeCookie("title"); 
      $.removeCookie("id"); 
      $.removeCookie("desc"); 
      $.removeCookie("views"); 
      $.removeCookie("dur"); 
      $.removeCookie("tags"); 
      $.removeCookie("playlistId"); 
     } 
    }); 
}); 

答えて

1

あなたが検索結果にitemを参照しています。 ビデオの取得リクエストを発行すると、ビデオコンテンツの詳細と統計情報の応答が得られます。どうやら、返されたレスポンスをキャプチャしていないようです。

$.get(
        "https://www.googleapis.com/youtube/v3/videos", 
        { 
         part: 'contentDetails, statistics', 
         id: videoId, //item.snippet.resourceId.videoId, 
         key: 'XXXXXXXXX', 
        }, 

        function(videoItem) 
        { 
         vidDuration = videoItem.contentDetails.duration; // pass item's duration 
         viewCount = videoItem.statistics.viewCount; // pass item's view count 
        } 
       ); 

注:一つのリクエストで複数のビデオcontentDetails /統計情報を取得するために、カンマで区切られた動画IDの文字列を渡すことができます。

+1

コードフォームで例を教えてください。ありがとうございました! – TheAmazingKnight

+0

私は自分のコードを編集し、あなたが言ったことに基づいてvidDurationとviewCountで 'relatedVids(videoId)'関数と 'return'キーワードのための一つのパラメータしか持たないというようないくつかのものを洗練しましたが、' $ .get 'vidDuration'と' viewCount'を取得するためのvideoIdのオブジェクトはまだ存在しますが、両方の値には何も残っておらず、nullのままです。 – TheAmazingKnight

+0

@TheAmazingKnightあなたは私のポイントを得ていませんでした。最初に、 'videos' get要求のコールバック関数にはパラメータがありませんでした。つまり、返されたレスポンスを黙って無視します。そこで、あなたのコードを修正して、パラメータ 'videoItem'をコールバックに渡しました。 –

関連する問題