Ali*_*Tor 5 c# streaming mp3 duration
我想在下载开始时获取远程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;
}
Run Code Online (Sandbox Code Playgroud)
有点晚了,但如果其他人需要的话......
这篇 CodeProject文章有很多关于 MP3 标头的好方法。
total frames(如果存在,则为 4 个字节,大端字节序)。每个 MPEG 帧给出每帧恒定数量的样本,由采样率决定,无论帧中总字节数是多少。您可以通过如下计算进行估计:
durationVBR = single_frame_time * total_frames;
Run Code Online (Sandbox Code Playgroud)
在哪里...
single_frame_time = (SampleRate / SamplesPerFrame) * 1000;
Run Code Online (Sandbox Code Playgroud)
的常数SamplesPerFrame是:
MPEG-1
MPEG-2
| 归档时间: |
|
| 查看次数: |
396 次 |
| 最近记录: |