小站开通了: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;
///
/// 开始位置
///
public long StartPos
{
get { return _startPos; }
}
private string _header;
///
/// 8 byte ;
///
public string Header
{
get { return _header; }
}
private int _size;
///
/// 4 byte ;
///
public int Size
{
get { return _size; }
}
private string _version;
///
/// 4 byte ;
///
public string Version
{
get { return _version; }
}
private int _itemCount;
///
/// 4 byte ;
///
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();
}
///
/// 读取标签
///
///
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;
///
/// 标签大小
///
public int Size
{
get { return _size; }
}
private uint _flag;
///
/// 标签Flag
///
public uint Flag
{
get { return _flag; }
}
private string _frameID;
///
/// 标签名称
///
public string FrameID
{
get { return _frameID; }
}
private string _value;
///
/// 标签名称
///
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);
}
}
}