MFormats SDK: simple file playback

MFormats SDK can play back multiple file types right out of the box. This post shows how easy it is to implement in your application.

The simpliest GUI for file playback should have a preview panel, a control for loading files and a "Play" button.

The actual decoding is handled by the MFReaderClass object. Preview for video is handled by the MFPreviewClass.

We will only need these 2 objects to implement file


MFReaderClass m_objReader = new MFReaderClass();
MFPreviewClass m_objPreview = new MFPreviewClass();

To initialize the preview:

//Configure preview
m_objPreview.PreviewWindowSet("", panelPreview.Handle.ToInt32());
m_objPreview.PreviewEnable("", 1, 1); // enable audio and video preview

Each media file is a sequence of frames. In MFormats each video frame also contains the corresponding chunk of audio data - this way we make sure that audio and video are always in sync.

To work with frames we will need a timer:

private Timer playTimer;
//use somewhere in your application to initialize a timer
Timer playTimer = new Timer();
playTimer = new Timer();
playTimer.Interval = 10;
playTimer.Tick += playTimer_Tick;

So by the timer's tick we need to get a frame from a source file and display it on preview. This is the reason why we use such a small interval for the timer - to avoid missing frames. The highest frame rate for a file is around 30 frames per second. In other words, the user sees all these 30 frames in 1 second. So for 1 frame there is at least 30 msec for decoding and preview. So for example purposes we will use 10 msec as the interval for the timer.

But before getting the frame from the file we need to load this file to MFReaderClass object. It will take just a couple of lines:

m_objMFReader = new MFReaderClass();
m_objMFReader.ReaderOpen(textBox1.Text, ""); // where textBox1 is a TextBox where user inputs a path to a required file

The magic "Play" button will do only one thing. It should start the timer:

private void buttonPlay_Click(object sender, EventArgs e)

So the main method in our application is the timer's tick:

private void timerPlayback_Tick(object sender, EventArgs e)
	playTimer.Stop(); // to avoid GUI hangs
	if (m_objMFReader != null && m_objPreview != null)
	MFFrame pFrame;
	m_objMFReader.SourceFrameGetByTime(-1, -1, out pFrame, ""); // get a frame from source file
	((IMFReceiver)m_objPreview).ReceiverFramePut(pFrame, -1, ""); //Send frame to the preview
	Marshal.ReleaseComObject(pFrame); //to prevent memory leakage
	GC.Collect(); //you should call these methods to release MFrame object

That's it. As you can see, it is really simple to playback files with MFormats SDK.