This tutorial creates a Windows Forms application that opens a video file with MFReader and shows it with
MFPreview. It also sets up the project that the other MFormats tutorials use. For the difference between
MFormats and MPlatform, see About MFormats and MPlatform.
Create the project
- Install MFormats SDK.
- In Visual Studio, create a Windows Forms App project in C#.
- Set the platform to x64 (Build › Configuration Manager › Active solution platform › New › x64). The process loads the SDK components of its own bitness, so do not leave the choice to AnyCPU.
- Add the reference: Project › Add › COM Reference › MFormats Type Library. The
project gets a
COMReferencetoMFORMATSLib. - On the form, place a
PanelnamedpreviewPaneland aButtonnamedopenButton.
Play the file
The reader gives frames, the preview shows them. A worker thread moves the frames in a loop: it takes a frame from the
reader and puts it to the preview. The preview shows each frame at its time and holds ReceiverFramePut
until then, so the loop runs at the frame rate of the file.
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using MFORMATSLib;
public partial class Form1 : Form
{
private readonly MFReaderClass m_reader = new MFReaderClass();
private readonly MFPreviewClass m_preview = new MFPreviewClass();
private CancellationTokenSource m_cancel;
private Thread m_worker;
public Form1()
{
InitializeComponent();
m_preview.PreviewWindowSet("", previewPanel.Handle.ToInt64());
m_preview.PreviewEnable("", 1, 1); // audio and video
m_reader.PropsSet("loop", "true"); // start again at the end of the file
openButton.Click += OpenButton_Click;
}
private void OpenButton_Click(object sender, EventArgs e)
{
using var dialog = new OpenFileDialog();
if (dialog.ShowDialog() != DialogResult.OK)
return;
StopPlayback();
m_reader.ReaderOpen(dialog.FileName, "");
m_cancel = new CancellationTokenSource();
CancellationToken token = m_cancel.Token;
m_worker = new Thread(() => PlayLoop(token)) { IsBackground = true, Name = "Playback" };
m_worker.Start();
}
private void PlayLoop(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
m_reader.SourceFrameGet(-1, out MFFrame frame, "");
try
{
m_preview.ReceiverFramePut(frame, -1, "");
}
finally
{
Marshal.ReleaseComObject(frame);
}
}
}
private void StopPlayback()
{
if (m_worker == null)
return;
m_cancel.Cancel();
m_worker.Join();
m_cancel.Dispose();
m_worker = null;
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
StopPlayback();
m_reader.ReaderClose();
Marshal.ReleaseComObject(m_reader);
Marshal.ReleaseComObject(m_preview);
base.OnFormClosed(e);
}
}
What the code does
-
PreviewWindowSetgives the preview the window to draw in. Call it again when the panel changes its size or handle. -
ReaderOpenopens a file or a network stream (for exampleudp://orsrt://); the second argument takes reader properties. -
SourceFrameGet(-1, …)waits for the next frame without a time limit. -
Marshal.ReleaseComObject(frame)releases every frame at once. A frame holds video and audio memory; leaving frames to the garbage collector quickly uses up memory. - The loop stops through a
CancellationToken. Do not stop it withThread.Abort: in .NET 5 and later it throwsPlatformNotSupportedException, and in .NET Framework it can interrupt the thread inside an SDK call.
Next steps
- Use the same loop with a device instead of a file: MFormats 2: Capturing from a device.
- The properties of the reader are listed in MFReader object properties.
- The SDK sample
Tutorial File Playbackshows the same with a timer and a thread.
Still have questions?
Tell us what didn’t work or what you’d like clarified.