Question

I keep getting an array out of bounds exception with Taglib.tag.Performers in this function that edits ID3 data. I read elsewhere that clearing tag.performers[] can help (if null) but I still get the error sometimes.

Error message:

"Index was outside the bounds of the array.Data:

'System.Collections.ListDictionaryInternal' for test.mp3"

var fileArr = Directory.GetFiles(BasePath, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".mp3") || s.EndsWith(".m4a")).ToArray();

foreach (var file in fileArr)
{
    string fileName = Path.GetFileName(file);
    string tagArtist = "";
    string tagTitle = "";
    string tempRegFilename = fileName;
    string title = "";

    //Apply to tag
    TagLib.File mp3tag = TagLib.File.Create(file);

    if (mp3tag.Tag.Title != null && mp3tag.Tag.Title.Length > 1)
    {
        title = mp3tag.Tag.Title;
    } 
    else
    {
        mp3tag.Tag.Title = String.Empty;
    }

    if (mp3tag.Tag.Performers[0].Length < 1 || mp3tag.Tag.Performers[0] == null)
    {
        mp3tag.Tag.Performers[0] = null;
        mp3tag.Tag.Performers = new[] { String.Empty };
        mp3tag.Save();
    }

    if (mp3tag.Tag.Performers[0].Length > 1)
    {
        string[] performers = mp3tag.Tag.Performers;
        if (title.Length > 2 && performers[0].Length > 1)
        {
            tagTitle = title;
            tagArtist = performers[0].ToString();
            Log.Info("ID3 Artist: " + "[" + tagArtist + "]");
            Log.Info("ID3 Title: " + "[" + tagTitle + "]");
            Log.Info("Tag data OK");
        }
    }
    //Get artist from filename
    if (mp3tag.Tag.Performers[0].Length < 1 || mp3tag.Tag.Performers == null)
    {
        mp3tag.Tag.Performers = new[] { String.Empty };
        string prevArtist = String.Empty;

        if (tempRegFilename.Contains("-"))
        {
            Log.Info("Artist data missing...");
            string[] words = tempRegFilename.Split('-');
            {
                words[0] = words[0].Trim();
                string perf = words[0];
                mp3tag.Tag.Performers = new[] { perf };
                Log.Info("Artists changed from \'" + prevArtist + "\' to " + "'" + perf + "'" + "\r\n");
                mp3tag.Save();
            }
        }
        mp3tag.Save();
    }
}

catch (Exception ex)
{
    Log.Error("TAG EXCEPTION: " + ex.Message + "Data: " + "'" + ex.Data + "'" + " for " + fileName + "\r\n" + ex.HelpLink);
}

Can anyone see what's wrong? I don't have much experience and could use the help. Thanks.

Was it helpful?

Solution

You seem to be assuming in a number of places that mp3Tag.Tag.Performers will have at least one element in it. If it doesn't, then you'll get the exception that you mention whenever you try to access mp3tag.Tag.Performers[0]

It looks like you may be trying to catch that possibility with this code:

if (mp3tag.Tag.Performers[0].Length < 1 || mp3tag.Tag.Performers[0] == null)
{
    mp3tag.Tag.Performers[0] = null;
    mp3tag.Tag.Performers = new[] { String.Empty };
    mp3tag.Save();
}

But your logic is incorrect: you're getting the first element from the array (apparently a string) and checking its length, instead of checking the length of the array itself. Try this:

if (mp3tag.Tag.Performers.Length < 1 || mp3tag.Tag.Performers[0] == null)
{
    mp3tag.Tag.Performers = new[] { String.Empty };
    mp3tag.Save();
}

PS: It'll be much easier for you to see where your errors are if your log includes the stack trace of the exception, rather than just its message. I typically find it best to just use the ToString() on the exception itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top