編輯

共用方式為


Quickstart: Use the Bing Video Search .NET client library

Use this quickstart to begin searching for news with the Bing Video Search client library for C#. While Bing Video Search has a REST API compatible with most programming languages, the client library provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub with additional annotations, and features.

Prerequisites

To add the Bing Video Search client library to your project, select Manage NuGet Packages from Solution Explorer in Visual Studio. Add the Microsoft.Azure.CognitiveServices.Search.VideoSearch package.

Installing the NuGet Video Search SDK package also installs the following dependencies:

  • Microsoft.Rest.ClientRuntime
  • Microsoft.Rest.ClientRuntime.Azure
  • Newtonsoft.Json

Create and initialize a project

  1. Create a new C# console solution in Visual Studio. Then add the following into the main code file.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Azure.CognitiveServices.Search.VideoSearch;
    using Microsoft.Azure.CognitiveServices.Search.VideoSearch.Models;
    
  2. Instantiate the client by creating a new ApiKeyServiceClientCredentials object with your subscription key, and then calling the constructor.

    var client = new VideoSearchAPI(new ApiKeyServiceClientCredentials("YOUR-ACCESS-KEY"));
    

Send a search request and process the results

  1. Use the client to send a search request. Use "SwiftKey" for the search query.

    var videoResults = client.Videos.SearchAsync(query: "SwiftKey").Result;
    
  2. If any results were returned, get the first one with videoResults.Value[0]. Then print the video's ID, title, and URL.

    if (videoResults.Value.Count > 0)
    {
        var firstVideoResult = videoResults.Value[0];
    
        Console.WriteLine($"\r\nVideo result count: {videoResults.Value.Count}");
        Console.WriteLine($"First video id: {firstVideoResult.VideoId}");
        Console.WriteLine($"First video name: {firstVideoResult.Name}");
        Console.WriteLine($"First video url: {firstVideoResult.ContentUrl}");
    }
    else
    {
        Console.WriteLine("Couldn't find video results!");
    }
    

Next steps

See also