1

I tried to get the the length of a Video i searched before, with the youtube v3 API you can download at nuget.org.

I know there are a lot of solution, but they are always written in php.

That is the code I am using Right now:

var searchListRequest = youtubeService.Search.List("snippet");
earchListRequest.Q = Find;
searchListRequest.MaxResults = 5;



var searchListResponse = await searchListRequest.ExecuteAsync();
foreach (var searchResult in searchListResponse.Items)
        {
            switch (searchResult.Id.Kind)
            {
                case "youtube#video":
                break;
            }
        }

Thanks for any kind of help :)

2
  • And what issue you are facing with this code? Can you share the php code which inspired you to write this code? Commented Jan 19, 2018 at 10:22
  • I need some way to access the Duration of the SearchResult. I used this code: developers.google.com/youtube/v3/code_samples/dotnet Commented Jan 19, 2018 at 10:24

1 Answer 1

2

You can donwlaod the Json version of the video :

    WebClient myDownloader = new WebClient();
    myDownloader.Encoding = System.Text.Encoding.UTF8;

   string jsonResponse=myDownloader.DownloadString(
   "https://www.googleapis.com/youtube/v3/videos?id=" + yourvideoID+ "&key=" 
   + youtubekey + "&part=contentDetails");
   dynamic dynamicObject = Json.Decode(jsonResponse);
   string tmp = dynamicObject.items[0].contentDetails.duration;
   var Duration = Convert.ToInt32
   (System.Xml.XmlConvert.ToTimeSpan(tmp).TotalSeconds);
Sign up to request clarification or add additional context in comments.

3 Comments

It marks me Json.Decode. I assume i have to install some kind of nuget?
you have to install this on nuget : nuget.org/packages/Google.Apis.YouTube.v3/1.32.0.1079, then use "resolve..." in visual studio as usual
replace "var" dynamicObject with "dynamic" dynamicObject and here is the documentation of Json.Decode : msdn.microsoft.com/en-us/library/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.