Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play audio from a stream using C#

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);                     }                 }             }         }     } 
like image 255
Martin Avatar asked Oct 08 '08 20:10

Martin


People also ask

How do I extract audio from a stream?

If you use Chrome, a good option is Video Downloader. And if you use Firefox, try DownloadHelper. Using VLC to rip the audio track from a streaming video you've downloaded to your PC. Once you have the video file saved to your machine, you can use any number of programs to save out the audio track.

How do I stream to my stereo?

The easiest way to adapt an old stereo for streaming is to add a Bluetooth receiver. This lets you stream wirelessly from a phone, tablet, or computer without having to add or configure a new app. The downside is that Bluetooth does have a subtle effect on sound quality.

How do I stream my desktop audio?

Go to Windows Control Panel and search for Sound and select it. On the Sound window, select the Recording tab. Right click on Microphone in the list and tick the Show Disabled Devices and Show Disconnected Devices menu items. Now, Stereo Mix (and other devices) will appear in the list.


1 Answers

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.

like image 66
Mark Heath Avatar answered Sep 20 '22 14:09

Mark Heath