Programming in C#

15. Playing media

In Visual Studio, in the Toolbox, there is no obvious media control available. But Windows comes with Windows Media Player and part of that comes with its own libraries and built in controls which can be referenced within the Visual Studio.

To add this control, open the Project menu, select Add Reference, and browse for the following two files in C:\Windows\System32 called WMP.dll (Windows Media Library Sharing Services) and WMPdxm.dll (Windows Media Player Compatibility Layer).

WMP References

In the Toolbox, right click it and select 'Choose Items', and select in COM components tab the Windows Media Player entry.

WMP Control

You will now have a windows media control on your windows form.

The control has one major property and that is the URL, which you can specify the location, path and filename of the sound, or video you wish to play, for example, via a 'Load File' button.

private void button1_Click(object sender, EventArgs e)
{
    string mediaFile;

    openFileDialog1.ShowDialog();
    mediaFile = openFileDialog1.FileName;
    axWindowsMediaPlayer1.URL = mediaFile;
}