使用C#从流播放音频 [英] Play audio from a stream using C#

查看:2057
本文介绍了使用C#从流播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有在C#的方式从一个<一个direcly播放音频(例如,MP3)的href="http://msdn.microsoft.com/en-us/library/system.io.stream%28v=vs.110%29.aspx">System.IO.Stream这例如是从一个的WebRequest returend而不暂时保存数据到磁盘?


n音讯

解决方案

随着 n音讯 1.3的帮助,可以:

  1. 加载从URL MP3文件到MemoryStream
  2. 在MP3数据转换成波形数据后,这是完全加载
  3. 播放使用波数据 n音讯的waveout的类

这本来是很高兴能连打半加载的MP3文件,但这似乎是不可能的,因为在 n音讯的库设计。

这是做这项工作的功能:

 公共静态无效PlayMp3FromUrl(字符串URL)
    {
        使用(流毫秒=新的MemoryStream())
        {
            使用(流流= WebRequest.Create(URL)
                .GetResponse()。GetResponseStream())
            {
                byte []的缓冲区=新的字节[32768]
                INT读取;
                而((读取= stream.Read(缓冲液,0,buffer.Length))大于0)
                {
                    ms.Write(缓冲,0,读);
                }
            }

            ms.Position = 0;
            使用行为(WaveStream blockAlignedStream =
                新BlockAlignReductionStream(
                    WaveFor​​matConversionStream.CreatePcmStream(
                        新Mp3FileReader(毫秒))))
            {
                使用(waveout的waveout的=新waveout的(WaveCallbackInfo.FunctionCallback()))
                {
                    waveOut.Init(blockAlignedStream);
                    waveOut.Play();
                    而(waveOut.PlaybackState == PlaybackState.Playing)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                }
            }
        }
    }
 

解决方案

编辑:答案更新,以反映变化在最近n音讯版本

这是可能使用 n音讯开源.NET声音我已经写库。它看起来对你的电脑进行转换的一个ACM codeC。与n音讯供给的Mp3FileReader目前期望能够对源数据流中重新定位(它建立的MP3帧的前场的指数),所以它不是合适的流在网络上。但是,你仍然可以使用 MP3Frame AcmMp3FrameDecom pressor ​​类n音讯到DECOM preSS流MP3的飞行。

我已经贴在我的博客一篇文章,解释的如何播放使用n音讯一个MP3流。从本质上讲,你有一个线程下载的MP3帧,DECOM pressing他们并将其存储在一个 BufferedWaveProvider 。另一个线程然后播放使用 BufferedWaveProvider 作为输入。

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?


Solution with NAudio

With the help of NAudio 1.3 it is possible to:

  1. Load an MP3 file from a URL into a MemoryStream
  2. Convert MP3 data into wave data after it was completely loaded
  3. Playback the wave data using NAudio's WaveOut class

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.

And this is the function that will do the work:

    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);
                    }
                }
            }
        }
    }

解决方案

Edit: Answer updated to reflect changes in recent versions of NAudio

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.

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#从流播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆