如何在NAudio MP3流媒体部分使用轨迹栏

时间:2018-06-21 08:59:27

标签: c# naudio

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

我可以在Mp3Stream中使用轨迹栏吗?因为当我在do命令上设置断点时,readFullyStream变量中的CanSeek属性为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();
}

1 个答案:

答案 0 :(得分:0)

NAudio流演示仅在MP3帧到达时解压缩并播放。没有代码可以存储以前接收的音频。因此,如果您希望具有倒带功能,则需要实施该功能。

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