I have a list of music files in my listbox and I'm trying to play them using the media player when I double click on them. But it keeps coming up with the error. I'm assuming it's because 'files' is not a string, but how do I convert it into a string? I've tried using .ToString but it doesn't work. I'm quite new to this. Any help is appreciated.
The error is in the axWindowsMediaPlayer.URL = files[listBox1.SelectedIndex];
Here is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] extensions = new[] { ".mp3", ".wma", ".wav", ".MP3", ".WMA" };
FileInfo[] files;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex];
}
private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Tracks");
files = dinfo.EnumerateFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();
for (int i = 0; i < files.Length; i++)
{
listBox1.Items.Add(files[i]);
}
}
This is the code that's showing the error:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex];
}
I don't have an IDE open to try this, but try changing:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex];
}
to:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex].Fullname;
}