MP3TAG:APEV2

小站开通了:http://noteba.com/

 

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Collections;

namespace WindowsFormsApplication1
{
    public class APEV2
    {
        private MP3Stream mp3Stream;

        private long _startPos;
        /// <summary>
        /// 开始位置    
        /// </summary>
        public long StartPos
        {
            get { return _startPos; }
        }

        private string _header;
        /// <summary>
        /// 8 byte  ;
        /// </summary>
        public string Header
        {
            get { return _header; }
        }

        private int _size;
        /// <summary>
        /// 4 byte  ;
        /// </summary>
        public int Size
        {
            get { return _size; }
        }


        private string _version;
        /// <summary>
        /// 4 byte  ;
        /// </summary>
        public string Version
        {
            get { return _version; }
        }

        private int _itemCount;
        /// <summary>
        /// 4 byte  ;
        /// </summary>
        public int ItemCount
        {
            get { return _itemCount; }
        }


        private ArrayList _tagItems = new ArrayList();
        public ArrayList TagItems
        {
            get { return _tagItems; }
        }

        public APEV2(MP3Stream stream)
        {
            mp3Stream = stream;

            _startPos = stream.Position;           

            _header = mp3Stream.ReadText(8, Encoding.ASCII);
            _version = mp3Stream.ReadUInt2(4).ToString();

            _size = (int)mp3Stream.ReadUInt2(4);           
            _itemCount = (int)mp3Stream.ReadUInt2(4);

            mp3Stream.Seek(4, SeekOrigin.Current);//Global flags of all items

            mp3Stream.Seek(8, SeekOrigin.Current);//Must be zero  skiped;Reserved

            mp3Stream.Seek(0 - _size, SeekOrigin.Current);//from root to tagItem StartPos
                       

            ReadTag();
        }

        /// <summary>
        /// 读取标签
        /// </summary>
        /// <returns></returns>
        private void ReadTag()
        {
            int length = _size - 32;//substract the length of tag foot ;           
            while (length > 10 && _tagItems.Count < _itemCount )
            {
                TagItem tagItem = new TagItem(mp3Stream);
                _tagItems.Add(tagItem);

                length -= (tagItem.Size + 10);           
            }

            mp3Stream.Seek(32, SeekOrigin.Current);//skip tag foot;
           
        }

    }


    public class TagItem
    {       

        private int _size;
        /// <summary>
        /// 标签大小     
        /// </summary>
        public int Size
        {
            get { return _size; }
        }


        private uint _flag;
        /// <summary>
        /// 标签Flag     
        /// </summary>
        public uint Flag
        {
            get { return _flag; }
        }

        private string _frameID;
        /// <summary>
        /// 标签名称
        /// </summary>
        public string FrameID
        {
            get { return _frameID; }
        }

        private string _value;
        /// <summary>
        /// 标签名称
        /// </summary>
        public string Value
        {
            get { return _value; }
        }
      

        public TagItem(MP3Stream stream)
        {
            _size = (int)stream.ReadUInt2(4);
            _flag = stream.ReadUInt2(4);


            int count = 0;
            while (stream.ReadByte() != 0)
            {
                count++;
            }
            if (count > 0)
            {
                stream.Seek(0 - count -1, SeekOrigin.Current);
                _frameID = stream.ReadText(count, Encoding.ASCII);
            }
            else {
                stream.Seek(-1, SeekOrigin.Current);
                _frameID = "";
            }
            stream.Seek(1, SeekOrigin.Current);//skip 0x00;
           
            _value = stream.ReadText(_size, Encoding.Default);

        }

    }

}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页