示例#1
0
        /// <summary>
        /// Saves the metadata associated with this audio file from its properties.
        /// </summary>
        public void SaveMetadata()
        {
            // Check what is the type of the audio file
            if (fileType == AudioFileFormat.MP3)
            {          
                // Declare variables
                TagLib.Mpeg.AudioFile file = null;

                try
                {
                    // Read tags
                    file = new TagLib.Mpeg.AudioFile(filePath);                    

                    // Copy tags
                    file = (TagLib.Mpeg.AudioFile)FillTags(file);

                    // Save metadata
                    file.Save();
                }
                catch (Exception ex)
                {
                    // Throw exception
                    throw new Exception("An error occured while reading/writing the tags and properties of the file (" + filePath + ").", ex);
                }
                finally
                {
                    // Dispose file (if needed)
                    if (file != null)
                        file.Dispose();
                }
            }
            else if (fileType == AudioFileFormat.FLAC)
            {
                // Declare variables
                TagLib.Flac.File file = null;

                try
                {
                    // Read tags
                    file = new TagLib.Flac.File(filePath);

                    // Copy tags
                    file = (TagLib.Flac.File)FillTags(file);

                    // Save metadata
                    file.Save();
                }
                catch (Exception ex)
                {
                    // Throw exception
                    throw new Exception("An error occured while reading/writing the tags and properties of the file (" + filePath + ").", ex);
                }
                finally
                {
                    // Dispose file (if needed)
                    if (file != null)
                        file.Dispose();
                }
            }
            else if (fileType == AudioFileFormat.OGG)
            {
                // Declare variables
                TagLib.Ogg.File file = null;

                try
                {
                    // Read tags
                    file = new TagLib.Ogg.File(filePath);

                    // Copy tags
                    file = (TagLib.Ogg.File)FillTags(file);

                    // Save metadata
                    file.Save();
                }
                catch (Exception ex)
                {
                    // Throw exception
                    throw new Exception("An error occured while reading/writing the tags and properties of the file (" + filePath + ").", ex);
                }
                finally
                {
                    // Dispose file (if needed)
                    if (file != null)
                        file.Dispose();
                }
            }
            else if (fileType == AudioFileFormat.APE)
            {
                // Declare variables
                TagLib.Ape.File file = null;

                try
                {
                    // Read tags
                    file = new TagLib.Ape.File(filePath);

                    // Copy tags
                    file = (TagLib.Ape.File)FillTags(file);

                    // Save metadata
                    file.Save();
                }
                catch (Exception ex)
                {
                    // Throw exception
                    throw new Exception("An error occured while reading/writing the tags and properties of the file (" + filePath + ").", ex);
                }
                finally
                {
                    // Dispose file (if needed)
                    if (file != null)
                        file.Dispose();
                }
            }
            else if (fileType == AudioFileFormat.WV)
            {
                // Declare variables
                TagLib.WavPack.File file = null;

                try
                {
                    // Read tags
                    file = new TagLib.WavPack.File(filePath);

                    // Copy tags
                    file = (TagLib.WavPack.File)FillTags(file);

                    // Save metadata
                    file.Save();
                }
                catch (Exception ex)
                {
                    // Throw exception
                    throw new Exception("An error occured while reading/writing the tags and properties of the file (" + filePath + ").", ex);
                }
                finally
                {
                    // Dispose file (if needed)
                    if (file != null)
                        file.Dispose();
                }
            }
            else if (fileType == AudioFileFormat.WMA)
            {
                // Declare variables
                TagLib.Asf.File file = null;

                try
                {
                    // Read tags
                    file = new TagLib.Asf.File(filePath);

                    // Copy tags
                    file = (TagLib.Asf.File)FillTags(file);

                    // Save metadata
                    file.Save();
                }
                catch (Exception ex)
                {
                    // Throw exception
                    throw new Exception("An error occured while reading/writing the tags and properties of the file (" + filePath + ").", ex);
                }
                finally
                {
                    // Dispose file (if needed)
                    if (file != null)
                        file.Dispose();
                }
            }
        }