Integrating Cloudflare Video Streaming Service with MVC C#

Cloudflare Video Streaming Service is a platform that allows users to stream high-quality videos on their websites. It is a reliable and secure platform that provides features like adaptive bitrate streaming, video encoding, and content delivery network (CDN) capabilities. In this blog, we will explore how to integrate Cloudflare Video Streaming Service with an MVC C# application.

Prerequisites

Before we begin, there are a few prerequisites that need to be fulfilled:

  1. A Cloudflare Video Streaming Service account
  2. An MVC C# application
  3. Visual Studio installed on your system

Integration Steps Now that we have fulfilled the prerequisites, we can proceed with the integration steps.

Step 1: Create a new MVC C# project in Visual Studio

Open Visual Studio and create a new MVC C# project. Name it whatever you like and select the target framework version.

Step 2: Install Cloudflare.Video.SDK package

In order to integrate Cloudflare Video Streaming Service with your MVC C# application, you need to install the Cloudflare.Video.SDK package. To do so, open the Package Manager Console and run the following command:

Install-Package Cloudflare.Video.SDK

This will install the Cloudflare.Video.SDK package in your project.

Step 3: Configure Cloudflare Video Streaming Service

To configure Cloudflare Video Streaming Service, you need to create an API token. Follow the steps below to create an API token:

  1. Log in to your Cloudflare account.
  2. Go to the ‘API Tokens’ page.
  3. Click the ‘Create Token’ button.
  4. Select the ‘Edit’ template and give it a name.
  5. Under the ‘Account Resources’ section, select ‘Stream’.
  6. Click the ‘Create Token’ button.

Now that you have created an API token, you need to add it to your MVC C# application. Open the ‘appsettings.json’ file and add the following code:

"CloudflareVideo": {
    "ApiToken": "API_TOKEN",
    "AccountId": "ACCOUNT_ID"
}

Replace API_TOKEN with the API token you just created and ACCOUNT_ID with your Cloudflare account ID.

Step 4: Create a new video on Cloudflare Video Streaming Service

To create a new video on Cloudflare Video Streaming Service, you need to use the Cloudflare.Video.SDK package. Open the HomeController.cs file and add the following code to the ‘Index’ method:

using Cloudflare.Video.SDK;
using Cloudflare.Video.SDK.Models;
using Microsoft.Extensions.Configuration;

public class HomeController : Controller
{
    private readonly IConfiguration _configuration;

    public HomeController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public async Task<IActionResult> Index()
    {
        var apiToken = _configuration["CloudflareVideo:ApiToken"];
        var accountId = _configuration["CloudflareVideo:AccountId"];

        var client = new CloudflareVideoClient(apiToken, accountId);

        var video = new Video
        {
            Title = "My video",
            Description = "This is my first video",
            Encoding = Encoding.PRESET_AVC_BASELINE_720P_16X9,
            Player = Player.DEFAULT
        };

        var result = await client.CreateVideoAsync(video);

        ViewBag.VideoId = result.Result.Id;

        return View();
    }
}

This code creates a new video with the specified title, description, encoding, and player. It then returns the video ID, which is stored in the ViewBag and passed to the view.

Step 5: Add Cloudflare Video Streaming Service player to your view To add the Cloudflare Video Streaming Service player to your view, you need to use the video ID that was returned in the previous step. Open the Index.cshtml file and add the following code:

@{
    ViewData["Title"] = "Home Page";
}

<h2>@ViewData["Title"]</h2>

@if (ViewBag.VideoId != null)
{
    <div class="player-wrapper">
        <video id="video-player" class="video-player" playsinline controls>
            <source src="https://videodelivery.net/VIDEO_ID/master.m3u8" type="application/x-mpegURL">
        </video>
    </div>

    <script src="https://embed.videodelivery.net/embed/sdk/latest/cloudflare-video.min.js"></script>
    <script>
        var player = new CloudflareVideo.Player({
            target: '#video-player',
            token: 'API_TOKEN',
            playerId: 'PLAYER_ID',
            accountId: 'ACCOUNT_ID',
            videoId: '@ViewBag.VideoId'
        });

        player.init();
    </script>
}

Replace VIDEO_ID with the video ID that was returned in the previous step, API_TOKEN with your Cloudflare API token, PLAYER_ID with the ID of the player you want to use, and ACCOUNT_ID with your Cloudflare account ID.

This code adds a video player to your view and initializes it with the specified parameters.

Conclusion

In this blog, we explored how to integrate Cloudflare Video Streaming Service with an MVC C# application. We learned how to install the Cloudflare.Video.SDK package, configure Cloudflare Video Streaming Service, create a new video, and add the Cloudflare Video Streaming Service player to our view. By following these steps, you can easily add video streaming capabilities to your MVC C# application using Cloudflare Video Streaming Service.

Spread the love

Similar Posts