0
Answer

Saving and Loading Media Player

Michael Singh

Michael Singh

8y
385
1
Hi All,
 
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. Another is that, if a user selects a media the first time, they can multi-select and load it. If the user decides to load another song, the media player crashes and has an error of,
 
"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; // Enables the user to choose multiple files.
      if (newPlaylist.ShowDialog() == System.Windows.Forms.DialogResult.OK)
      {
            files = newPlaylist.SafeFileNames; // Sets the variable 'files' to get the actual filename of the             media file, which is the title of the file a user sees.
            paths = newPlaylist.FileNames; // Sets the variable 'paths' to get the actual location/path             where the file is located/placed.
            for (int list = 0; list < files.Length; list++) // A for-statement that will check how many files             where chosen.
            {
                  lbPlaylist.Items.Add(files[list]); // Adds the files to the list box as a listbox item.
            }
        }
}
 
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(); // Simply opens up the save file dialog window.
            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;
      }
}
 
 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.