System.IndexOutOfRangeException for a Windows media player, MP3 player error

karam bultaif 20 Reputation points
2023-07-31T16:12:07.5233333+00:00

Hi, I have an issue with the Windows player library. I can add a selected mp3 format, and I can play it normally. But when I try to add another mp3 file, the error "System.IndexOutOfRangeException" appears. And it highlights line 34, where it says "Player.URL = paths[Name_File.SelectedIndex];". I'll post the code here, if you need anything else just ask me.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComputerScienceAIForm
{
    public partial class RecordInterface : Form
    {
        public RecordInterface()
        {
            InitializeComponent();
            trackBar1.Value = 50;
        }
        string[] paths, files;

        private void Name_File_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void BT_Play_Click(object sender, EventArgs e)
        {
            Player.Ctlcontrols.play();
        }

        private void Name_File_SelectedIndexChanged(object sender, EventArgs e)
        {
            Player.URL = paths[Name_File.SelectedIndex];
            Player.Ctlcontrols.play();
        }

        private void BT_Pause_Click(object sender, EventArgs e)
        {
            Player.Ctlcontrols.pause();
        }

        private void Stop_Button_Click(object sender, EventArgs e)
        {
            Player.Ctlcontrols.stop();
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (Player.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                p_Bar.Maximum = (int)Player.Ctlcontrols.currentItem.duration;
                p_Bar.Value = (int)Player.Ctlcontrols.currentPosition;
            }
            try
            {
                lbl_Track_Start.Text = Player.Ctlcontrols.currentPositionString;
                lbl_Track_end.Text = Player.Ctlcontrols.currentItem.durationString.ToString();
            }
            catch
            {

            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            Player.settings.volume = trackBar1.Value;
            VolumePercent.Text = trackBar1.Value.ToString() + "%";
        }

        private void p_Bar_MouseDown(object sender, MouseEventArgs e)
        {
            Player.Ctlcontrols.currentPosition = Player.currentMedia.duration * e.X / p_Bar.Width;
        }

        private void SettingsLabel_Click(object sender, EventArgs e)
        {

        }

        private void SafeButton_Click(object sender, EventArgs e)
        {
            Name_File.Items.RemoveAt(Name_File.SelectedIndex);
           
        }

        private void RecordInterface_Load(object sender, EventArgs e)
        {
            Name_File.Text = Properties.Settings.Default.ListAudio1;
        }

        private void RecordInterface_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.ListAudio1 = Name_File.Text;
            Properties.Settings.Default.Save();
        }

        private void BT_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                files = ofd.SafeFileNames;
                paths = ofd.FileNames;
                
            }

            for (int lc = 0; lc < files.Length; lc++)
            {
                Name_File.Items.Add(files[lc]);
            }
            

        }
    }
}
Windows for business Windows Client for IT Pros User experience Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-08-01T07:15:13.2966667+00:00

    Hi @karam bultaif , Welcome to Microsoft Q&A.

    Your code has several problems.

    You could not use arrays like this in c#.

    C# arrays are not mutable.

    Use List<string> instead of array.

    And when you use Name_File.Items.RemoveAt(Name_File.SelectedIndex); it fires Name_File.Items.RemoveAt(Name_File.SelectedIndex);.

    Reckless judgment has to be added to it.

    The following is my simplified code, which works after my tests. for reference only:

    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace _8_xx_xx
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
        
            }
            List<string> paths, files;
            private void SafeButton_Click(object sender, System.EventArgs e)
            {
                int index = Name_File.SelectedIndex;
                if (index == -1)
                    return;
                Name_File.Items.RemoveAt(index);
                files.RemoveAt(index);
                paths.RemoveAt(index);
            }
    
            private void Open_Click(object sender, System.EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Multiselect = true;
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    //First time
                    if (files == null || paths == null)
                    {
                        files = ofd.SafeFileNames.ToList();
                        paths = ofd.FileNames.ToList();
                    }
                    //Not first time
                    else
                    {
                        files.AddRange(ofd.SafeFileNames.ToList());
                        paths.AddRange(ofd.FileNames.ToList());
                    }
                }
                Name_File.Items.Clear();
                for (int lc = 0; lc < files.Count; lc++)
                {
                    Name_File.Items.Add(files[lc]);
                }
            }
            private void Name_File_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                //Check if there is a selected item
                if (Name_File.SelectedIndex == -1)
                    return;
                Player.URL = paths[Name_File.SelectedIndex];
                Player.Ctlcontrols.play();
            }
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


3 additional answers

Sort by: Most helpful
  1. Limitless Technology 44,746 Reputation points
    2023-08-01T10:46:41.22+00:00

    Hello there,

    The "System.IndexOutOfRangeException" error is a common exception in programming, and it can occur in various scenarios. When it specifically relates to a Windows Media Player or MP3 player, it is likely caused by trying to access an element in an array or collection using an index that is out of its valid range.

    Here are some possible reasons for this error and steps you can take to troubleshoot it:

    Invalid Index Value: Double-check the code or configuration that triggers the error and verify if the index value being used to access an array or collection is within the valid range. Remember that array indices typically start from 0, so if the code uses a value greater than the length of the array, it will throw an IndexOutOfRangeException.

    Empty or Null Collection: Ensure that the array or collection being accessed is not empty or null. If the collection is empty or doesn't contain any elements, attempting to access its elements using an index will lead to this error.

    Iteration Errors: If the error occurs during a loop or iteration, verify that the loop's condition and increment are correct and won't cause the index to go out of bounds.

    Boundary Checks: If you're working with external APIs or libraries that return arrays or collections, make sure to check the documentation to understand their expected size or length and handle them appropriately.

    I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

  2. tennakoon mudiyanselage amarasinha 0 Reputation points
    2023-08-13T07:12:04.8266667+00:00

    PLS MP3 MIX PLAYER CONNECT WINDOWS MIDEA PLAYER

    0 comments No comments

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.