Changing Tags

Aug 16, 2012 at 9:20 AM

How does one go about changing tags as all properties are read only!

Coordinator
Aug 17, 2012 at 9:06 PM
Edited Aug 17, 2012 at 9:08 PM

As soon as you access the read-only frame property, the corresponding frame structure is created in-memory and added to the tag object. This was done to help avoid the need for null checks and simplify the programming model.

For example, if you wanted to access the Title property, you could do one of the following, regardless of whether a Title frame existed:

//Using the implicit cast operator
string title = tag.Title;

//Using the Value property
string title = tag.Title.Value;

You do not need to explicitly check whether the tag.Title property is null or not; in fact, in this model, the property will never be null. If the frame did not exist in the tag, then the above code would return an empty string.

Automatically adding empty frames to the tag will not cause any side effects. These are only in-memory, and if you were to write the tag back to the MP3 file, empty frames would be ignored.

If your logic requires that you check whether the frame exists in the tag, then you can used the frame's IsAssigned property like this:

if (tag.Title.IsAssigned)
{
    //your logic here
}

Thanks for the question. Hope this helps.

Aug 17, 2012 at 9:15 PM

In my scenario I'm reading in the tags from an mp3 and want to write back changes to the mp3.

Basically I want to set the album property for each mp3 but the tag object property for album is readonly, so how do I set the value of the album property and write that back to the mp3?

 

Kind regards

Coordinator
Aug 17, 2012 at 10:11 PM

Try something like this:

using (var mp3 = new Mp3File("MySong.mp3", Mp3Permissions.ReadWrite))
{
    //Get the ID3v2 tag (currently only v2.3 supported)
    Id3Tag tag = mp3.GetTag(2, 3);
    if (tag == null)
        return;

    //Assign the new album name
    tag.Album.Value = "New album name";

    //Save the tag back to the MP3
    mp3.WriteTag(tag);
}

Aug 18, 2012 at 12:04 PM

I can't believe I missed the .value!

Thanks for your help!