Example #1
0
        //Retrieves the tag as a byte array. This method does not attempt to read the tag data,
        //it simply reads the header and if present the tag bytes are read directly from the
        //stream. This means that typical exceptions that get thrown in a tag read will not
        //occur in this method.
        public byte[] GetTagBytes(int majorVersion, int minorVersion)
        {
            RegisteredId3Handler registeredHandler = RegisteredHandlers.GetHandler(majorVersion, minorVersion);

            byte[] tagBytes = registeredHandler.Handler.GetTagBytes(_stream);
            return(tagBytes);
        }
Example #2
0
        /// <summary>
        /// Deletes the ID3 tag of the specified version from the MP3 data.
        /// </summary>
        /// <param name="majorVersion">The major version number.</param>
        /// <param name="minorVersion">The minor version number.</param>
        public void DeleteTag(int majorVersion, int minorVersion)
        {
            EnsureWritePermissions(Id3Messages.NoWritePermissions_CannotDeleteTag);
            RegisteredId3Handler registeredHandler = ExistingHandlers.GetHandler(majorVersion, minorVersion);

            if (registeredHandler != null)
            {
                registeredHandler.Handler.DeleteTag(_stream);
                InvalidateExistingHandlers();
            }
        }
Example #3
0
        /// <summary>
        /// Deletes the ID3 tag of the specified tag family type from the MP3 data.
        /// </summary>
        /// <param name="family">The ID3 tag family type.</param>
        public void DeleteTag(Id3TagFamily family)
        {
            EnsureWritePermissions(Id3Messages.NoWritePermissions_CannotDeleteTag);
            IEnumerable <RegisteredId3Handler> registeredHandlers = ExistingHandlers.GetHandlers(family);
            RegisteredId3Handler registeredHandler = registeredHandlers.FirstOrDefault();

            if (registeredHandler != null)
            {
                Id3Handler handler = registeredHandler.Handler;
                handler.DeleteTag(_stream);
                InvalidateExistingHandlers();
            }
        }
Example #4
0
        /// <summary>
        /// Retrieves an ID3 tag of the specified tag family type - version 2.x or version 1.x.
        /// </summary>
        /// <param name="family">The ID3 tag family type required.</param>
        /// <returns>The ID3 tag of the specified tag family type, or null if it doesn't exist.</returns>
        public Id3Tag GetTag(Id3TagFamily family)
        {
            IEnumerable <RegisteredId3Handler> familyHandlers = ExistingHandlers.GetHandlers(family);

            RegisteredId3Handler familyHandler = familyHandlers.FirstOrDefault();

            if (familyHandler == null)
            {
                return(null);
            }
            Id3Handler handler = familyHandler.Handler;
            Id3Tag     tag     = handler.ReadTag(_stream);

            return(tag);
        }
Example #5
0
        public bool WriteTag(Id3Tag tag, WriteConflictAction conflictAction = WriteConflictAction.NoAction)
        {
            EnsureWritePermissions(Id3Messages.NoWritePermissions_CannotWriteTag);
            if (tag == null)
            {
                throw new ArgumentNullException("tag");
            }

            //The tag should specify major version number
            if (tag.MajorVersion == 0)
            {
                throw new ArgumentException(Id3Messages.MajorTagVersionMissing, "tag");
            }

            //Get any existing handlers from the same family as the tag
            IEnumerable <RegisteredId3Handler> familyHandlers = ExistingHandlers.GetHandlers(tag.Family);

            //If a tag already exists from the same family, but is a different version than the passed tag,
            //delete it if conflictAction is Replace.
            RegisteredId3Handler familyHandler = familyHandlers.FirstOrDefault();

            if (familyHandler != null)
            {
                Id3Handler handler = familyHandler.Handler;
                if (handler.MajorVersion != tag.MajorVersion || handler.MinorVersion != tag.MinorVersion)
                {
                    if (conflictAction == WriteConflictAction.NoAction)
                    {
                        return(false);
                    }
                    if (conflictAction == WriteConflictAction.Replace)
                    {
                        Id3Handler handlerCopy = handler;
                        handlerCopy.DeleteTag(_stream);
                    }
                }
            }

            //Write the tag to the file. The handler will know how to overwrite itself.
            RegisteredId3Handler registeredHandler = RegisteredHandlers.GetHandler(tag.MajorVersion, tag.MinorVersion);
            bool writeSuccessful = registeredHandler.Handler.WriteTag(_stream, tag);

            if (writeSuccessful)
            {
                InvalidateExistingHandlers();
            }
            return(writeSuccessful);
        }
Example #6
0
        //Converts an ID3 tag to another version after resolving the differences between the two
        //versions. The resultant tag will have all the frames from the source tag, but those
        //frames not recognized in the new version will be treated as UnknownFrame objects.
        //Similarly, frames recognized in the output tag version, but not in the source version are
        //converted accordingly.
        public Id3Tag ConvertTo(int majorVersion, int minorVersion)
        {
            if (MajorVersion == majorVersion && MinorVersion == minorVersion)
            {
                return(this);
            }
            RegisteredId3Handler sourceHandler = Mp3Stream.RegisteredHandlers.GetHandler(MajorVersion, MinorVersion);

            if (sourceHandler == null)
            {
                return(null);
            }
            RegisteredId3Handler destinationHandler = Mp3Stream.RegisteredHandlers.GetHandler(majorVersion, minorVersion);

            if (destinationHandler == null)
            {
                return(null);
            }
            Id3Tag destinationTag = destinationHandler.Handler.CreateTag();

            foreach (Id3Frame sourceFrame in Frames)
            {
                var unknownFrame = sourceFrame as UnknownFrame;
                if (unknownFrame != null)
                {
                    string   frameId          = unknownFrame.Id;
                    Id3Frame destinationFrame = destinationHandler.Handler.GetFrameFromFrameId(frameId);
                    destinationTag.Frames.Add(destinationFrame);
                }
                else
                {
                    destinationTag.Frames.Add(sourceFrame);
                }
            }
            return(destinationTag);
        }
Example #7
0
        /// <summary>
        /// Retrieves an ID3 tag of the specified version number.
        /// </summary>
        /// <param name="majorVersion">The major version number.</param>
        /// <param name="minorVersion">The minor version number</param>
        /// <returns>The ID3 tag of the specified version number, or null if it doesn't exist.</returns>
        public Id3Tag GetTag(int majorVersion, int minorVersion)
        {
            RegisteredId3Handler registeredHandler = ExistingHandlers.GetHandler(majorVersion, minorVersion);

            return(registeredHandler != null?registeredHandler.Handler.ReadTag(_stream) : null);
        }