我想在下载开始时获取远程mp3文件的持续时间信息。我可以先获取帧,但不知道哪些必须读取,Xing或VBRi。
如何通过阅读标签获取此信息?
MemoryStream ms = new MemoryStream();
waveOut.Play();
long offset = from;
ms.Position = 0;
byte[] decBuffer = new byte[50 * 1024];
while (true)
{
if (paused)
{
waveOut.Stop();
bwProvider.ClearBuffer();
break;
}
lock (LockObj)
{
byte[] readed = Helper.ReadStreamPartially(localStream, offset, 100 * 1024, orders);
if (readed == null)
continue;
ms = new MemoryStream(readed);
}
Mp3Frame frame;
try
{
frame = Mp3Frame.LoadFromStream(ms);
}
catch
{
continue;
}
if (frame == null)
continue;
int decompressed = decompressor.DecompressFrame(frame, decBuffer, 0);
bwProvider.AddSamples(decBuffer, 0, decompressed);
if (Helper.IsBufferNearlyFull(bwProvider))
Thread.Sleep(500);
offset += ms.Position;
}
答案 0 :(得分:1)
有点晚了,但如果有人需要它......
这个CodeProject article有很多关于MP3标题的好形成。
total frames
的整数(如果存在将是4个字节,Big endian)。每个MPEG帧每帧提供恒定数量的采样,由采样率决定,而不管总 bytes-in-frameX 。您可以通过以下计算进行估算:
durationVBR = single_frame_time * total_frames;
WHERE ...
single_frame_time = (SampleRate / SamplesPerFrame) * 1000;
SamplesPerFrame
的常量是:
<强> MPEG-1 强>
<强> MPEG-2 强>