Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C#

A Simple C# Wrapper for the AviFile Library

Rate me:
Please Sign up or sign in to vote.
4.94/5 (210 votes)
12 Nov 2012CPOL9 min read 2M   72K   511  
Edit AVI files in .NET.
/* This class has been written by
 * Corinna John (Hannover, Germany)
 * cj@binary-universe.net
 * 
 * You may do with this code whatever you like,
 * except selling it or claiming any rights/ownership.
 * 
 * Please send me a little feedback about what you're
 * using this code for and what changes you'd like to
 * see in later versions. (And please excuse my bad english.)
 * 
 * WARNING: This is experimental code.
 * Please do not expect "Release Quality".
 * */

#region Using directives

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

#endregion

namespace AviFile {
    public class AviPlayer {

        private VideoStream videoStream;
        private PictureBox picDisplay;
        private Control ctlFrameIndexFeedback;
        private int millisecondsPerFrame;
        private bool isRunning;
        private int currentFrameIndex;
        private Bitmap currentBitmap;

        private delegate void SimpleDelegate();
        public event EventHandler Stopped;

        /// <summary>Returns the current playback status</summary>
        public bool IsRunning {
            get { return isRunning; }
        }

        /// <summary>Create a new AVI Player</summary>
        /// <param name="videoStream">Video stream to play</param>
        /// <param name="picDisplay">PictureBox to display the video</param>
        /// <param name="ctlFrameIndexFeedback">Optional Label to show the current frame index</param>
        public AviPlayer(VideoStream videoStream, PictureBox picDisplay, Control ctlFrameIndexFeedback) {
            this.videoStream = videoStream;
            this.picDisplay = picDisplay;
            this.ctlFrameIndexFeedback = ctlFrameIndexFeedback;
            this.isRunning = false;
        }

        /// <summary>Start the video playback</summary>
        public void Start() {
            isRunning = true;
            millisecondsPerFrame = (int)(1000 / videoStream.FrameRate);
            Thread thread = new Thread(new ThreadStart(Run));
            thread.Start();
        }

        /// <summary>Extract and display the frames</summary>
        private void Run() {
            videoStream.GetFrameOpen();

            for (currentFrameIndex = 0; (currentFrameIndex < videoStream.CountFrames) && isRunning; currentFrameIndex++) {
                //show frame
                currentBitmap = videoStream.GetBitmap(currentFrameIndex);
                picDisplay.Invoke(new SimpleDelegate(SetDisplayPicture));
                picDisplay.Invoke(new SimpleDelegate(picDisplay.Refresh));

                //show position
                if (ctlFrameIndexFeedback != null) {
                    ctlFrameIndexFeedback.Invoke(new SimpleDelegate(SetLabelText));
                }

                //wait for the next frame
                Thread.Sleep(millisecondsPerFrame);
            }

            videoStream.GetFrameClose();
            isRunning = false;

            if (Stopped != null) {
                Stopped(this, EventArgs.Empty);
            }
        }

        /// <summary>Change the visible frame</summary>
        private void SetDisplayPicture() {
            picDisplay.Image = currentBitmap;
        }

        /// <summary>Change the frame index feedback</summary>
        private void SetLabelText() {
            ctlFrameIndexFeedback.Text = currentFrameIndex.ToString();
        }

        /// <summary>Stop the video playback</summary>
        public void Stop() {
            isRunning = false;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
Corinna lives in Hanover/Germany and works as a C# developer.

Comments and Discussions