This tutorial creates a Windows Forms application that plays a video file with MFile. In MPlatform the file
object plays by itself in its own threads: the application opens the file, starts it and controls it. It also sets up
the project that the other MPlatform tutorials use. For the difference between MFormats and MPlatform, see
About MFormats and MPlatform.
Create the project
- Install MPlatform 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 › MPlatform Type Library. The
project gets a
COMReferencetoMPLATFORMLib. - On the form, place a
PanelnamedpreviewPaneland buttonsopenButton,pauseButtonandfastButton.
Play the file
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using MPLATFORMLib;
public partial class Form1 : Form
{
private readonly MFileClass m_file = new MFileClass();
private bool m_paused;
public Form1()
{
InitializeComponent();
m_file.PreviewWindowSet("", previewPanel.Handle.ToInt64());
m_file.PreviewEnable("", 1, 1); // audio and video
m_file.PropsSet("loop", "true"); // start again at the end of the file
openButton.Click += OpenButton_Click;
pauseButton.Click += PauseButton_Click;
fastButton.Click += FastButton_Click;
}
private void OpenButton_Click(object sender, EventArgs e)
{
using var dialog = new OpenFileDialog();
if (dialog.ShowDialog() != DialogResult.OK)
return;
m_file.FileNameSet(dialog.FileName, "");
m_file.FilePlayStart();
m_paused = false;
}
private void PauseButton_Click(object sender, EventArgs e)
{
if (m_paused)
m_file.FilePlayStart();
else
m_file.FilePlayPause(0);
m_paused = !m_paused;
}
private void FastButton_Click(object sender, EventArgs e)
{
m_file.FileRateSet(2.0); // double speed; 1.0 is normal
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
m_file.ObjectClose();
Marshal.ReleaseComObject(m_file);
base.OnFormClosed(e);
}
}
What the code does
-
PreviewWindowSetandPreviewEnableturn on the preview built into the file object. CallPreviewWindowSetagain when the panel changes its handle. -
FileNameSetopens a file or a network stream (for exampleudp://orsrt://); the second argument takes properties of the file reader. -
FilePlayStart,FilePlayPauseandFilePlayStopcontrol the playback;FilePosSet(seconds, 0)moves to a position,FileRateSetchanges the speed. -
ObjectClosestops the object and frees the file. Release every MPlatform object withMarshal.ReleaseComObjectwhen you no longer need it.
No loop and no thread are needed: the object keeps the timing itself. To process the frames in your code, subscribe to
the OnFrameSafe event of the object.
Next steps
- Use a device as the source: MPlatform 2: Capturing from a device.
- The SDK sample
Network Playback Sampleplays files and network streams with these calls.
Still have questions?
Tell us what didn’t work or what you’d like clarified.