Skip to content

Commit 9979d59

Browse files
authored
Merge pull request #254 from hwahrmann/Property_preventing_autocreate_ID3_TagTypes
Property preventing autocreate id3 tag types
2 parents 61402a9 + a116ed5 commit 9979d59

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

src/TaglibSharp.Tests/FileFormats/Id3BothFormatTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,29 @@ public void TestRemoveTags ()
109109
file.RemoveTags (TagTypes.AllTags);
110110
Assert.AreEqual (TagTypes.None, file.TagTypes);
111111
}
112+
113+
[Test]
114+
public void TestCreateId3Tags ()
115+
{
116+
string tempFile = TestPath.Samples + "tmpwrite_sample_createid3tags.mp3";
117+
118+
System.IO.File.Copy (sample_file, tempFile, true);
119+
120+
// Remove All Tags first
121+
var file = File.Create (tempFile);
122+
file.RemoveTags (TagTypes.AllTags);
123+
file.Save ();
124+
125+
// No TagTypes should exist
126+
TagLib.Mpeg.AudioFile.CreateID3Tags = false;
127+
file = File.Create (tempFile);
128+
Assert.AreEqual (TagTypes.None, file.TagTypes);
129+
file.Save ();
130+
131+
// Empty TagTypes should be created
132+
TagLib.Mpeg.AudioFile.CreateID3Tags = true;
133+
file = File.Create (tempFile);
134+
Assert.AreEqual (TagTypes.Id3v1 | TagTypes.Id3v2, file.TagTypes);
135+
}
112136
}
113137
}

src/TaglibSharp/Mpeg/AudioFile.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public class AudioFile : TagLib.NonContainer.File
6868
#endregion
6969

7070

71+
#region Private Static Fields
72+
73+
/// <summary>
74+
/// Specifies whether or not to create ID3v1 and
75+
/// ID3v2 tags when they don't exist..
76+
/// </summary>
77+
private static bool create_id3_tags = true;
78+
79+
#endregion
80+
7181

7282
#region Constructors
7383

@@ -154,6 +164,33 @@ public AudioFile (IFileAbstraction abstraction)
154164
#endregion
155165

156166

167+
#region Public Static Properties
168+
169+
/// <summary>
170+
/// Gets and sets whether or not to create ID3v1 and
171+
/// ID3v2 tags automatically when they are not existing.
172+
/// </summary>
173+
/// <value>
174+
/// <see langword="true" /> if tags to be created automatically.
175+
/// Otherwise, <see langword="false" />.
176+
/// </value>
177+
/// <remarks>
178+
/// <para>Sometimes a MP3 file should only contain ID3v1 and no
179+
/// ID3v2 Tags. Or instead of ID3v2 Tags APE Tags should be used.
180+
/// By setting this property to <see langword="false" />,
181+
/// no ID3v1 and Id3v2 Tags will be created when creating the file,
182+
/// if they don't exist.
183+
/// They need to be created explicitly if needed.</para>
184+
/// <para>The default is <see langword="true" /> which means that
185+
/// ID3v1 and Id3v2 tags are created when they don't exist.</para>
186+
/// </remarks>
187+
public static bool CreateID3Tags {
188+
get { return create_id3_tags; }
189+
set { create_id3_tags = value; }
190+
}
191+
192+
#endregion
193+
157194

158195
#region Public Methods
159196

@@ -252,9 +289,9 @@ protected override void ReadStart (long start, ReadStyle propertiesStyle)
252289
/// </param>
253290
protected override void ReadEnd (long end, ReadStyle propertiesStyle)
254291
{
255-
// Make sure we have ID3v1 and ID3v2 tags.
256-
GetTag (TagTypes.Id3v1, true);
257-
GetTag (TagTypes.Id3v2, true);
292+
// Creation of ID3v1 and ID3v2 tags based on CreateID3Tags property
293+
GetTag (TagTypes.Id3v1, create_id3_tags);
294+
GetTag (TagTypes.Id3v2, create_id3_tags);
258295
}
259296

260297
/// <summary>

0 commit comments

Comments
 (0)