0
votes

Beginner here with C# coding. I built a media player that allows the administrator to create and save a playlist. The guest should be able to load the playlist and play the songs on the said playlist. Unfortunately, every time the guest loads a playlist created, error pops out when trying to play any songs on the playlist. It seems to only load the path details but not the actual media.

"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in WindowsFormsApplication1.exe

Additional information: Index was outside the bounds of the array."

This is the code under the list box I have:

axWindowsMediaPlayer1.URL = paths[lbPlaylist.SelectedIndex]; 

The code code below is coded under the "Create Playlist" button:

private void btnCreate_Click(object sender, EventArgs e)
{
      OpenFileDialog newPlaylist = new OpenFileDialog();
      newPlaylist.InitialDirectory = "C:\\Users\\mklsingh\\Documents\\Visual Studio 2013\\Projects\\Media Player\\WindowsFormsApplication1\\Media Files";
      newPlaylist.Filter = "MP3 Audio File (*.mp3)|*.mp3| Windows Media File (*.wma)|*.wma|WAV Audio File (*.wav)|*.wav|All Files (*.*)|*.*";
      newPlaylist.RestoreDirectory = false;
      newPlaylist.Multiselect = true;
      if (newPlaylist.ShowDialog() == System.Windows.Forms.DialogResult.OK)
      {
            files = newPlaylist.SafeFileNames;
            paths = newPlaylist.FileNames; 
            for (int list = 0; list < files.Length; list++) 
            {
                  lbPlaylist.Items.Add(files[list]); 
            }
        }
}

The code below saves the playlist as XML file:

private void btnSave_Click(object sender, EventArgs e)
{
      StreamWriter Write;
      SaveFileDialog savePlaylist = new SaveFileDialog();
      savePlaylist.RestoreDirectory = false;
      try
      {
            savePlaylist.InitialDirectory = "C:\\Users\\mklsingh\\Documents\\Visual Studio 2013\\Projects\\Media Player\\WindowsFormsApplication1\\Media Files\\Playlist";
            savePlaylist.Filter = ("XML File|*.xml|All Files|*.*");
            savePlaylist.ShowDialog(); 
            Write = new StreamWriter(savePlaylist.FileName);
            for (int I = 0; I < lbPlaylist.Items.Count; I++)
            {
                  Write.WriteLine(lbPlaylist.Items[I]);
             }
            Write.Close();
            MessageBox.Show("Playlist saved!");
        }

      catch //(Exception ex)
      {
               return;
      }
}

The code below loads the saved XML playlist file:

private void btnLoad_Click(object sender, EventArgs e)
        {


            OpenFileDialog loadPlaylist = new OpenFileDialog();    
            loadPlaylist.Multiselect = false; 

            this.lbPlaylist.Items.Clear();   

            try
            {
                loadPlaylist.ShowDialog();
                loadPlaylist.InitialDirectory = "C:\\Users\\mklsingh\\Documents\\Visual Studio 2013\\Projects\\Media Player\\WindowsFormsApplication1\\Media Files\\Playlist";
                //txtLoad.Text = loadPlaylist.Filename;
                StreamReader playlist = new StreamReader(loadPlaylist.FileName); 

                while (playlist.Peek() >= 0) 
                    lbPlaylist.Items.Add(playlist.ReadLine());
                    txtLoad.Text = loadPlaylist.FileName; 
            }

            catch 
            {
                return;
            }          

        }

Also, if a user selects a single song and play it, it'll work. If the user decides to add a song on the current playlist, the selected song will not play and same error will pop out. If I click "Clear List" and select a new song, it'll work though.

Please let me know your thoughts on my code. I am still a beginner and I find it hard to understand some of the codes I see online. Haha. Just want to make my Save Playlist and Create Playlist button work. Thanks.

1
To start with you could just check to make sure the index you are trying to access exists. if( lbPlaylist.SelectedIndex < paths.Length) and then only try and get the path if it exists.nastassiar
This if-statement should be placed on the listbox? Sorry, really newbie here.Michael Singh

1 Answers

1
votes

Before you do

xWindowsMediaPlayer1.URL = paths[lbPlaylist.SelectedIndex]; 

Check to make sure selectedIndex actually exists in paths

if (lbPlaylist.SelectedIndex < paths.Length)
{
   xWindowsMediaPlayer1.URL = paths[lbPlaylist.SelectedIndex]; 
}
else
{
   // Display an error?
}

This should at least help you with debugging I would suggest putting a break point somewhere and checking to see what the value of paths is. It's possible you didn't initialize or add to it correct so you are trying to access an entry in the array that doesn't exist.