Playing Audio and Video Files In C#

Objective

To develop a Windows application for playing audio and video files using c#.net.

Design

Design

Design a form as above with an OpenFileDialog control, one Button, and the 'Windows Media Player' control (COM component).

Note that OpenFileDialog control appears below the form (not on the form), which is used in our application for browsing audio/video files.

Steps for adding 'Windows Media Player' control (COM component) into Toolbox:

By default, the 'Windows Media Player' control is not provided in the Toolbox, we have to add it into the toolbox if required.

In order to add the 'Windows Media Player' control into the toolbox.

Right-click on the 'General' tab (or any other tab) in the toolbox ->select 'Choose Items...' ->select 'COM Components' tab ->select 'Windows Media Player' ->click on the 'OK' button.

General

Toolbox

'Windows Media Player' control will appear in the toolbox.

Windows Media Player

Now, drag the 'Windows Media Player' control onto the form and place a button on it with text as 'Browse..' as shown in the design.

Code

using System;
using System.Windows.Forms;
namespace mymediaplayer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "(mp3,wav,mp4,mov,wmv,mpg)|*.mp3;*.wav;*.mp4;*.mov;*.wmv;*.mpg|all files|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
        }
    }
}

Output

Output


Similar Articles