使用 C# 从流中播放音频

编程入门 行业动态 更新时间:2024-10-28 18:34:59
本文介绍了使用 C# 从流中播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 C# 中是否有一种方法可以直接从 System.IO.Stream 例如从 WebRequest 返回而不将数据临时保存到磁盘?

Is there a way in C# to play audio (for example, MP3) direcly from a System.IO.Stream that for instance was returend from a WebRequest without saving the data temporarily to the disk?

在 NAudio 1.3 的帮助下,可以:

With the help of NAudio 1.3 it is possible to:

  • 从 URL 加载 MP3 文件到 MemoryStream
  • MP3 数据完全加载后转换成波形数据
  • 使用 NAudio 的 WaveOut 类播放波形数据
  • Load an MP3 file from a URL into a MemoryStream
  • Convert MP3 data into wave data after it was completely loaded
  • Playback the wave data using NAudio's WaveOut class
  • 如果能够播放一半加载的 MP3 文件就太好了,但由于 NAudio 库设计.

    It would have been nice to be able to even play a half loaded MP3 file, but this seems to be impossible due to the NAudio library design.

    这是完成工作的函数:

    public static void PlayMp3FromUrl(string url) { using (Stream ms = new MemoryStream()) { using (Stream stream = WebRequest.Create(url) .GetResponse().GetResponseStream()) { byte[] buffer = new byte[32768]; int read; while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } } ms.Position = 0; using (WaveStream blockAlignedStream = new BlockAlignReductionStream( WaveFormatConversionStream.CreatePcmStream( new Mp3FileReader(ms)))) { using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback())) { waveOut.Init(blockAlignedStream); waveOut.Play(); while (waveOut.PlaybackState == PlaybackState.Playing ) { System.Threading.Thread.Sleep(100); } } } } }

    推荐答案

    更新答案以反映 NAudio 最新版本的变化

    可以使用我编写的 NAudio 开源 .NET 音频库.它会在您的 PC 上寻找 ACM 编解码器来进行转换.NAudio 提供的 Mp3FileReader 当前期望能够在源流中重新定位(它预先构建 MP3 帧的索引),因此它不适合通过网络进行流式传输.但是,您仍然可以使用 NAudio 中的 MP3Frame 和 AcmMp3FrameDecompressor 类来动态解压缩流式 MP3.

    It's possible using the NAudio open source .NET audio library I have written. It looks for an ACM codec on your PC to do the conversion. The Mp3FileReader supplied with NAudio currently expects to be able to reposition within the source stream (it builds an index of MP3 frames up front), so it is not appropriate for streaming over the network. However, you can still use the MP3Frame and AcmMp3FrameDecompressor classes in NAudio to decompress streamed MP3 on the fly.

    我在我的博客上发布了一篇文章,解释了 如何使用 NAudio 播放 MP3 流.本质上,您有一个线程下载 MP3 帧,解压缩它们并将它们存储在 BufferedWaveProvider 中.然后另一个线程使用 BufferedWaveProvider 作为输入进行回放.

    I have posted an article on my blog explaining how to play back an MP3 stream using NAudio. Essentially you have one thread downloading MP3 frames, decompressing them and storing them in a BufferedWaveProvider. Another thread then plays back using the BufferedWaveProvider as an input.

    更多推荐

    使用 C# 从流中播放音频

    本文发布于:2023-07-11 12:39:19,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1094096.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:音频

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!