MP3TAG:APEV2

来源(cloudandy的专栏 - CSDN博客)

From: http://blog.csdn.net/cloudandy/archive/2009/05/01/4142488.aspx

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);

        }

    }

}




本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cloudandy/archive/2009/05/01/4142488.aspx

Link: http://www.asm32.net/article_details.aspx?id=4584


浏览次数 44 发布时间 2009-09-26 14:28:43 从属分类 C# 【评论】【 】【打印】【关闭
 
| www.asm32.net | 2006版 | 资料中心 | linux | asm/asm32 | C/C++ | VC++ | java | Python | 书签 | ASP.Net书签 | 京ICP备09029108号-1