How to play media files instead of watching a live video stream in C#?

This example demonstrates how to play media files instead of watching a live video stream in C#. This is a great option because you can implement filters, effects and detectors to the application effectively turning it to a video analyzing tool. For example you can rewiev security camera footings and analyze the image with our motion detector or face detector if it is needed or you can use our license plate recognizer if you are looking for a specific car on your video. To implement this example, you must have Ozeki Camera SDK installed, and a reference to OzekiSDK.dll should be added to your Visual Studio project.

Methods

Important: you should study this article in order to find out how to setup your Windows Forms correctly. The application currently works only with .mpeg4 files. In the future we will expand the supported formats.

Getting started

To get started it is recomended to Download and Install the latest version of Ozeki Camera SDK. After installation you can find the example code discussed in this page with full source code in the following location on your harddisk:

Download Ozeki Camera SDK: https://www.camera-sdk.com/https://camera-sdk.com/p_6513-download-onvif-ozeki-camera-sdk-for-c-sharp.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Motion_Detection_Viewer_Side_WF\AVPlayer_WF.sln

To compile this example you will need Microsoft Visual Studio installed on your computer.

The following article will present you how to turn your video viewer into a practical and useful video player.

First of all, you need to declare a AVPlayer object from the Ozeki Camera SDK:

private AVPlayer _player;
	

Of course, you need to initialize this object after the GUI components have been initialized. It will require a .avi file which it will play:

_player = new AVPlayer(filePath);
	

You need to connect the VideoChannel of your camera to the DrawingImageProvider object. You can do all of this using the MediaConnector object in the following way:

_connector.Connect(_player.VideoChannel, _provider);
	

Now, you can implement further features based on other tutorials, like a motion detector, different shape detectors, effects, etc.. Finally, you will have to start your AVPlayer object as well.

_player.Start();
btn_Start.Enabled = false;
btn_stop.Enabled = true;
btn_Pause.Enabled = true;
	

You can stop your AVPlayer object by clicking on the Stop button that can be seen on the figure below, too.

_player.Stop();
videoViewerWF1.Stop();
btn_stop.Enabled = false;
btn_Start.Enabled = true;
btn_Pause.Enabled = false;
	

You can also pause the AVPlayer object by clicking on the Pause button. Note that the buttons are enabled or disabled according to the current state of the video stream.

_player.Pause();
btn_Start.Enabled = true;    
	

Viewer side motion detection functionality in C#

Windows Form  

Windows forms version

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ozeki.Media;


namespace AVPlayer_WF
{
    public partial class Form1 : Form
    {

        private AVPlayer _player;
        private MediaConnector _connector;
        private DrawingImageProvider _provider;
        
        public Form1()
        {
            InitializeComponent();

            _connector = new MediaConnector();

            _provider = new DrawingImageProvider();

            videoViewerWF1.Start();
        }


        private void btn_Browser_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            DialogResult result = openFileDialog1.ShowDialog(); 
	            if (result == DialogResult.OK)
                {
		                string filePath = openFileDialog1.FileName;

                        text_Path.Text = openFileDialog1.SafeFileName;
                        btn_Start.Enabled = true;

                        videoViewerWF1.SetImageProvider(_provider);

                        _player = new AVPlayer(filePath);

                        _connector.Connect(_player.VideoChannel, _provider);


	            }
            
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            _player.Stop();
            videoViewerWF1.Stop();
            btn_stop.Enabled = false;
            btn_Start.Enabled = true;
            btn_Pause.Enabled = false;
            
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            _player.Start();
            btn_Start.Enabled = false;
            btn_stop.Enabled = true;
            btn_Pause.Enabled = true;
        }

        private void btn_Pause_Click(object sender, EventArgs e)
        {
            _player.Pause();
            btn_Start.Enabled = true;
        }
    }
}

		
Code 1 - Viewer side motion detection functionality in C#

GUI

avplayer in C#
Figure 1 - The graphical user interface of your application

Below you can find the code that belongs to the interface of the previously presented application. With the help of this section your Windows Forms Application will be able to work properly.

Form1.Designer.cs

namespace AVPlayer_WF
{
    partial class Form1
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.videoViewerWF1 = new Ozeki.Media.VideoViewerWF();
            this.btn_Browser = new System.Windows.Forms.Button();
            this.label = new System.Windows.Forms.Label();
            this.text_Path = new System.Windows.Forms.TextBox();
            this.btn_Start = new System.Windows.Forms.Button();
            this.btn_stop = new System.Windows.Forms.Button();
            this.btn_Pause = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // videoViewerWF1
            // 
            this.videoViewerWF1.BackColor = System.Drawing.Color.Black;
            this.videoViewerWF1.FlipMode = Ozeki.Media.FlipMode.None;
            this.videoViewerWF1.FrameStretch = Ozeki.Media.FrameStretch.Uniform;
            this.videoViewerWF1.FullScreenEnabled = true;
            this.videoViewerWF1.Location = new System.Drawing.Point(11, 38);
            this.videoViewerWF1.Name = "videoViewerWF1";
            this.videoViewerWF1.RotateAngle = 0;
            this.videoViewerWF1.Size = new System.Drawing.Size(320, 240);
            this.videoViewerWF1.TabIndex = 0;
            this.videoViewerWF1.Text = "videoViewerWF1";
            // 
            // btn_Browser
            // 
            this.btn_Browser.Location = new System.Drawing.Point(256, 9);
            this.btn_Browser.Name = "btn_Browser";
            this.btn_Browser.Size = new System.Drawing.Size(75, 23);
            this.btn_Browser.TabIndex = 1;
            this.btn_Browser.Text = "Browser";
            this.btn_Browser.UseVisualStyleBackColor = true;
            this.btn_Browser.Click += new System.EventHandler(this.btn_Browser_Click);
            // 
            // label
            // 
            this.label.AutoSize = true;
            this.label.Location = new System.Drawing.Point(12, 15);
            this.label.Name = "label";
            this.label.Size = new System.Drawing.Size(23, 13);
            this.label.TabIndex = 2;
            this.label.Text = "File";
            // 
            // text_Path
            // 
            this.text_Path.Location = new System.Drawing.Point(53, 12);
            this.text_Path.Name = "text_Path";
            this.text_Path.Size = new System.Drawing.Size(197, 20);
            this.text_Path.TabIndex = 3;
            // 
            // btn_Start
            // 
            this.btn_Start.Enabled = false;
            this.btn_Start.Location = new System.Drawing.Point(11, 284);
            this.btn_Start.Name = "btn_Start";
            this.btn_Start.Size = new System.Drawing.Size(75, 23);
            this.btn_Start.TabIndex = 4;
            this.btn_Start.Text = "Play";
            this.btn_Start.UseVisualStyleBackColor = true;
            this.btn_Start.Click += new System.EventHandler(this.btn_Start_Click);
            // 
            // btn_stop
            // 
            this.btn_stop.Enabled = false;
            this.btn_stop.Location = new System.Drawing.Point(256, 284);
            this.btn_stop.Name = "btn_stop";
            this.btn_stop.Size = new System.Drawing.Size(75, 23);
            this.btn_stop.TabIndex = 5;
            this.btn_stop.Text = "Stop";
            this.btn_stop.UseVisualStyleBackColor = true;
            this.btn_stop.Click += new System.EventHandler(this.btn_stop_Click);
            // 
            // btn_Pause
            // 
            this.btn_Pause.Enabled = false;
            this.btn_Pause.Location = new System.Drawing.Point(131, 284);
            this.btn_Pause.Name = "btn_Pause";
            this.btn_Pause.Size = new System.Drawing.Size(75, 23);
            this.btn_Pause.TabIndex = 6;
            this.btn_Pause.Text = "Pause";
            this.btn_Pause.UseVisualStyleBackColor = true;
            this.btn_Pause.Click += new System.EventHandler(this.btn_Pause_Click);
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(343, 317);
            this.Controls.Add(this.btn_Pause);
            this.Controls.Add(this.btn_stop);
            this.Controls.Add(this.btn_Start);
            this.Controls.Add(this.text_Path);
            this.Controls.Add(this.label);
            this.Controls.Add(this.btn_Browser);
            this.Controls.Add(this.videoViewerWF1);
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private Ozeki.Media.VideoViewerWF videoViewerWF1;
        private System.Windows.Forms.Button btn_Browser;
        private System.Windows.Forms.Label label;
        private System.Windows.Forms.TextBox text_Path;
        private System.Windows.Forms.Button btn_Start;
        private System.Windows.Forms.Button btn_stop;
        private System.Windows.Forms.Button btn_Pause;

    }
}
		
Code 2 - GUI example in C#

DISCLAIMER: Please note that the following features will only work if your IP camera supports the given function. You should check the user manual of your IP camera to make sure it supports the feature that you wish to implement in C#.

Related Pages

FAQ

Below you can find the answers for the most frequently asked questions related to this topic:

  1. What file formats can I play?

    We currently support the following formats: MPEG4
    We are constantly working on supporting more file formats. Check back often to see the new additions.

  2. I have not managed to build the solution. How to solve it?

    • Please set the Target framework property of the project to .NET 4.0.
    • You should add the System.Drawing.dll and the OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.
  3. I see a black screen after pressing start.

    Make sure that the program supports your file format. If it is not supported, check back later or try again after converting it to one of the supported formats.

More information