Skip to main content

Player sample

This guide will help you create a simple video player using the Media Blocks SDK .Net.

Required media blocks

To create a video player, you need to add the following blocks to the pipeline:

Pipeline creation

First, we need to create a pipeline.

using VisioForge.Core.MediaBlocks;

var pipeline = new MediaBlocksPipeline();

The UniversalSourceBlock can be used to play files and network streams.


To handle the pipeline errors, you can subscribe to the `OnError` event.

```csharp
pipeline.OnError += (sender, args) =>
{
Console.WriteLine(args.Message);
};

Also, you can subscribe to the OnStart and OnStop events.

Adding blocks

We need to create an instance of block settings to add most blocks. For UniversalSourceBlock, we need to pass the file path.

var sourceSettings = await UniversalSourceSettings.CreateAsync(new Uri(edFilename.Text));
var fileSource = new UniversalSourceBlock(sourceSettings);

During the UniversalSourceSettings creation, SDK will open the file and read the video/audio stream information.


Now, let's add the video renderer.

```csharp
var videoRenderer = new VideoRendererBlock(_pipeline, VideoView1);

The VideoRendererBlock constructor has two parameters: the pipeline and the VideoView control to render the video.

Finally, let's add the audio renderer.

We'll enumerate available audio renderers and select the first one.

var audioRenderers = await DeviceEnumerator.Shared.AudioOutputsAsync();
var audioRenderer = new AudioRendererBlock(audioRenderers[0]);

Connecting blocks

Now, we need to connect the blocks.

pipeline.Connect(fileSource.VideoOutput, videoRenderer.Input);
pipeline.Connect(fileSource.AudioOutput, audioRenderer.Input);

The UniversalSourceBlock has two outputs: VideoOutput and AudioOutput. We need to connect them to the video and audio renderers, respectively.

You can connect only one output if the file has only video or audio.

You can check the audio/video streams available using the ReadInfoAsync method of the UniversalSourceSettings class.

Start the pipeline

To start the pipeline, call the StartAsync method.

await pipeline.StartAsync();

You'll see the video from the file on the screen.

Stop the pipeline

To stop the pipeline, call the StopAsync method.

await pipeline.StopAsync();

Please check the complete source code on GitHub.