【问题标题】:How to use track bar in NAudio MP3 streaming section如何在 NAudio MP3 流媒体部分使用轨迹栏
【发布时间】:2018-06-21 08:59:26
【问题描述】:

我在 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();
}

【问题讨论】:

    标签: c# naudio


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      相关资源
      最近更新 更多