如何在 NAudio MP3 流媒体部分中使用跟踪栏



我在C#中使用NAudio库,我需要一个跟踪栏来控制音乐。在NAudio Mp3Stream示例项目中,我们有标准控件,例如播放,停止,暂停,音量,但没有跟踪栏。

我可以在 Mp3Stream 中使用跟踪栏吗?因为当我在 do 命令上设置断点时,CanSeek 属性在 readFullStream 变量中为 false。

如何在流媒体上使用跟踪栏?

using (var responseStream = resp.GetResponseStream())
{
var readFullyStream = new ReadFullyStream(responseStream);
do
{
if (IsBufferNearlyFull)
{
Debug.WriteLine("Buffer getting full, taking a break");
Thread.Sleep(500);
}
else
{
Mp3Frame frame;
try
{
frame = Mp3Frame.LoadFromStream(readFullyStream);
}
catch (EndOfStreamException)
{
fullyDownloaded = true;
// reached the end of the MP3 file / stream
break;
}
catch (WebException)
{
// probably we have aborted download from the GUI thread
break;
}
if (decompressor == null)
{
// don't think these details matter too much - just help ACM select the right codec
// however, the buffered provider doesn't know what sample rate it is working at
// until we have a frame
decompressor = CreateFrameDecompressor(frame);
bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat);
bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(20); // allow us to get well ahead of ourselves
//this.bufferedWaveProvider.BufferedDuration = 250;
}
int decompressed = decompressor.DecompressFrame(frame, buffer, 0);
//Debug.WriteLine(String.Format("Decompressed a frame {0}", decompressed));
bufferedWaveProvider.AddSamples(buffer, 0, decompressed);
}
} while (playbackState != StreamingPlaybackState.Stopped);
Debug.WriteLine("Exiting");
// was doing this in a finally block, but for some reason
// we are hanging on response stream .Dispose so never get there
decompressor.Dispose();
}

NAudio流媒体演示只是在MP3帧到达时解压缩并播放它们。没有代码来存储以前收到的音频。因此,如果您想要倒带功能,则需要实现它。

您可以考虑的一种选择是使用MediaFoundationReader,传入流 URL 作为路径。这可以在下载时播放音频,并且还应支持重新定位。

最新更新