Hi dnnyobi,
The simple way to play mp3 is using WMPLib.
Refer below steps.
1. Right click on the projects references folder and select Add References.
2. In COM tab search for Windows Media Player and add in your project.
Then refer below code.
C#
public partial class Form1 : Form
{
WMPLib.WindowsMediaPlayer wmplayer = new WMPLib.WindowsMediaPlayer();
public Form1()
{
InitializeComponent();
}
int i = 1;
private void Form1_Load(object sender, EventArgs e)
{
string[] filePaths = System.IO.Directory.GetFiles(Application.StartupPath.Replace("bin\\Debug", "") + "Files\\");
foreach (string filePath in filePaths)
{
string fileName = System.IO.Path.GetFileName(filePath);
Button btnFile = new Button();
btnFile.Name = "Button" + i;
btnFile.Text = fileName;
btnFile.Size = new System.Drawing.Size(200, 30);
btnFile.Location = new System.Drawing.Point(40, i * 40);
btnFile.Click += new EventHandler(Button1_Click);
Controls.Add(btnFile);
i++;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
wmplayer.controls.stop();
string filePath = Application.StartupPath.Replace("bin\\Debug", "") + "Files/" + (sender as Button).Text;
wmplayer.URL = filePath;
wmplayer.controls.play();
}
}
VB.Net
Public Class Form1
Private wmplayer As WMPLib.WindowsMediaPlayer = New WMPLib.WindowsMediaPlayer()
Private i As Integer = 1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filePaths As String() = System.IO.Directory.GetFiles(Application.StartupPath.Replace("bin\Debug", "") & "Files\")
For Each filePath As String In filePaths
Dim fileName As String = System.IO.Path.GetFileName(filePath)
Dim btnFile As Button = New Button()
btnFile.Name = "Button" & i
btnFile.Text = fileName
btnFile.Size = New System.Drawing.Size(200, 30)
btnFile.Location = New System.Drawing.Point(40, i * 40)
AddHandler btnFile.Click, AddressOf Button1_Click
Controls.Add(btnFile)
i += 1
Next
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
wmplayer.controls.stop()
Dim filePath As String = Application.StartupPath.Replace("bin\Debug", "") & "Files/" + (TryCast(sender, Button)).Text
wmplayer.URL = filePath
wmplayer.controls.play()
End Sub
End Class