If you want to play audio file and video file in windows form c# project then you should go for windows media player control. This is unchecked component of toolbox bydefault. If you want to add media player into the list , check my previous article.
There are various step to design player in windows form, these are:
Step-1: Add menuStrip control on the form, add 'File' text in it as a root also design sub child of the root item like 'open' and 'add to the playlist'. Looking like
Step-2 : Add Windows media player and ListBox on the form also set the properties of both controls
Window Media Player control --Name property = WindowsMediaPlayer1
ListBox Control -- Name property= listBox1
Step-3 : Double click on Open file menu item and handle raised event, also double click on Add to Playlist menu item.
Step-4 : Copy the code and use it
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace mediaplayer
{
public partial class Form1 : Form
{
string[] path, filename;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog filedialog = new OpenFileDialog();
if (filedialog .ShowDialog ()==DialogResult.OK)
{
WindowsMediaPlayer1.URL = filedialog.FileName;
WindowsMediaPlayer1.Ctlcontrols.play();
}
}
private void createPlaylistToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog filedialog = new OpenFileDialog();
filedialog.Multiselect = true;
if (filedialog .ShowDialog ()==DialogResult .OK)
{
filename = filedialog.SafeFileNames;
path = filedialog.FileNames;
for (int i = 0; i < filename.Length; i++)
{
listBox1.Items.Add(filename[i]);
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = path[listBox1.SelectedIndex];
}
}
}
There are various step to design player in windows form, these are:
Step-1: Add menuStrip control on the form, add 'File' text in it as a root also design sub child of the root item like 'open' and 'add to the playlist'. Looking like
Step-2 : Add Windows media player and ListBox on the form also set the properties of both controls
Window Media Player control --Name property = WindowsMediaPlayer1
ListBox Control -- Name property= listBox1
Step-3 : Double click on Open file menu item and handle raised event, also double click on Add to Playlist menu item.
Step-4 : Copy the code and use it
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace mediaplayer
{
public partial class Form1 : Form
{
string[] path, filename;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog filedialog = new OpenFileDialog();
if (filedialog .ShowDialog ()==DialogResult.OK)
{
WindowsMediaPlayer1.URL = filedialog.FileName;
WindowsMediaPlayer1.Ctlcontrols.play();
}
}
private void createPlaylistToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog filedialog = new OpenFileDialog();
filedialog.Multiselect = true;
if (filedialog .ShowDialog ()==DialogResult .OK)
{
filename = filedialog.SafeFileNames;
path = filedialog.FileNames;
for (int i = 0; i < filename.Length; i++)
{
listBox1.Items.Add(filename[i]);
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = path[listBox1.SelectedIndex];
}
}
}
Code Generate the following output