103

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);
                    }
                }
            }
        }
    }
dmtweigt
  • 83
  • 4
Martin
  • 10,738
  • 14
  • 59
  • 67
  • 4
    good to see you got it working. It wouldn't be too much work to get it properly playing back while streaming. The main issue is that the Mp3FileReader currently expects to know the length in advance. I'll look into adding a demo for the next version of NAudio – Mark Heath Oct 22 '08 at 15:11
  • @Mark Heath did you already solve the problem and added the demo in the current NAudio version or is it still in your pipline? – Martin Jan 28 '10 at 07:26
  • afraid not yet, although with changes made in NAudio 1.3 it won't require too much tweaking to get it working. – Mark Heath Jan 28 '10 at 14:54
  • Mark: Do I need to modify in NAudio to get it working, cause i just downloaded NAudio1.3 but it is accepting above code without change, but on other hand throwing exception that is says something like "ACM Conversion not possible". – Mubashar Mar 10 '11 at 11:28
  • by the way I am trying to play following http://translate.google.com/translate_tts?q=I+love+techcrunch – Mubashar Mar 10 '11 at 11:32
  • is this code snippet works like download the song then when completed, it will play? – Vincent Dagpin Apr 23 '13 at 08:08
  • Can you please take the solution out of your question and post it as an answer? – slugster Jan 24 '17 at 00:18

10 Answers10

59

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.

dmtweigt
  • 83
  • 4
Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • 4
    The [NAudio](https://naudio.codeplex.com) library is now shipping with an example application called [Mp3StreamingDemo](https://naudio.codeplex.com/SourceControl/latest#NAudioDemo/Mp3StreamingDemo/MP3StreamingPanel.cs) which should provide everything one will need to live stream an MP3 from the network. – Martin Nov 19 '13 at 07:35
  • Is it possible to use your library to live stream mic/line in input to an android device? – realtebo Mar 11 '15 at 14:55
10

The SoundPlayer class can do this. It looks like all you have to do is set its Stream property to the stream, then call Play.

edit
I don't think it can play MP3 files though; it seems limited to .wav. I'm not certain if there's anything in the framework that can play an MP3 file directly. Everything I find about that involves either using a WMP control or interacting with DirectX.

OwenP
  • 24,950
  • 13
  • 65
  • 102
  • 1
    I've been trying to find a .NET library that does MP3 encoding and decoding for years now, but I don't think it exists. – MusiGenesis Oct 20 '08 at 13:03
  • NAudio should do the job for you - at least for decodeing (see first post) – Martin Oct 24 '08 at 12:26
  • 4
    SoundPlayer has nasty issues with GC and will randomly play garbage unless you use the official Microsoft workaround (click Workarounds): http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=93642 – Roman Starkov Nov 07 '09 at 09:12
  • SoundPlayer + memoryStream has bug by desing. when you play first second or third time it adds weird noise in rhe middle of your audio. – bh_earth0 Nov 20 '16 at 17:23
  • Workaround link doesn't work anymore, but it's on Wayback Machine: http://web.archive.org/web/20120722231139/http://connect.microsoft.com/VisualStudio/feedback/details/93642/soundplayer-plays-a-corrupt-sound – Forestrf Aug 15 '20 at 22:42
6

Bass can do just this. Play from Byte[] in memory or a through file delegates where you return the data, so with that you can play as soon as you have enough data to start the playback..

slugster
  • 49,403
  • 14
  • 95
  • 145
2

I slightly modified the topic starter source, so it can now play a not-fully-loaded file. Here it is (note, that it is just a sample and is a point to start from; you need to do some exception and error handling here):

private Stream ms = new MemoryStream();
public void PlayMp3FromUrl(string url)
{
    new Thread(delegate(object o)
    {
        var response = WebRequest.Create(url).GetResponse();
        using (var stream = response.GetResponseStream())
        {
            byte[] buffer = new byte[65536]; // 64KB chunks
            int read;
            while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                var pos = ms.Position;
                ms.Position = ms.Length;
                ms.Write(buffer, 0, read);
                ms.Position = pos;
            }
        }
    }).Start();

    // Pre-buffering some data to allow NAudio to start playing
    while (ms.Length < 65536*10)
        Thread.Sleep(1000);

    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);
            }
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ReVolly
  • 126
  • 8
  • Did you test your code? If so, with which version of NAudio did it work? – Martin Feb 24 '11 at 10:29
  • 1
    In addition - your code does look like it is quite error-prone to threading problems. Are you sure you know what you are doing? – Martin Feb 24 '11 at 10:37
  • 1) Yes, I did. 2) With latest version. 3) It's just a proof of concept as I stated in my previous message. – ReVolly Feb 24 '11 at 13:45
  • How do you define "latest version"? Is it Version 1.3 or the source at revision 68454? – Martin Mar 04 '11 at 09:08
  • @Martin Sorry, haven't been here for a long time. The NAudio.dll I used has version 1.3.8. – ReVolly Jun 27 '11 at 14:26
  • How to make this thread safe? you cant change stream Position while reading and writing at the same time, yes? also if stream is endless or very long how you remove stuff from the front? – user007 Dec 24 '15 at 13:14
2

I've tweaked the source posted in the question to allow usage with Google's TTS API in order to answer the question here:

bool waiting = false;
AutoResetEvent stop = new AutoResetEvent(false);
public void PlayMp3FromUrl(string url, int timeout)
{
    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.PlaybackStopped += (sender, e) =>
                {
                    waveOut.Stop();
                };
                waveOut.Play();
                waiting = true;
                stop.WaitOne(timeout);
                waiting = false;
            }
        }
    }
}

Invoke with:

var playThread = new Thread(timeout => PlayMp3FromUrl("http://translate.google.com/translate_tts?q=" + HttpUtility.UrlEncode(relatedLabel.Text), (int)timeout));
playThread.IsBackground = true;
playThread.Start(10000);

Terminate with:

if (waiting)
    stop.Set();

Notice that I'm using the ParameterizedThreadDelegate in the code above, and the thread is started with playThread.Start(10000);. The 10000 represents a maximum of 10 seconds of audio to be played so it will need to be tweaked if your stream takes longer than that to play. This is necessary because the current version of NAudio (v1.5.4.0) seems to have a problem determining when the stream is done playing. It may be fixed in a later version or perhaps there is a workaround that I didn't take the time to find.

Community
  • 1
  • 1
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
1

NAudio wraps the WaveOutXXXX API. I haven't looked at the source, but if NAudio exposes the waveOutWrite() function in a way that doesn't automatically stop playback on each call, then you should be able to do what you really want, which is to start playing the audio stream before you've received all the data.

Using the waveOutWrite() function allows you to "read ahead" and dump smaller chunks of audio into the output queue - Windows will automatically play the chunks seamlessly. Your code would have to take the compressed audio stream and convert it to small chunks of WAV audio on the fly; this part would be really difficult - all the libraries and components I've ever seen do MP3-to-WAV conversion an entire file at a time. Probably your only realistic chance is to do this using WMA instead of MP3, because you can write simple C# wrappers around the multimedia SDK.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
1

I haven't tried it from a WebRequest, but both the Windows Media Player ActiveX and the MediaElement (from WPF) components are capable of playing and buffering MP3 streams.

I use it to play data coming from a SHOUTcast stream and it worked great. However, I'm not sure if it will work in the scenario you propose.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ramiro Berrelleza
  • 2,254
  • 1
  • 15
  • 27
1

I wrapped the MP3 decoder library and made it available for .NET developers as mpg123.net.

Included are the samples to convert MP3 files to PCM, and read ID3 tags.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
0

I've always used FMOD for things like this because it's free for non-commercial use and works well.

That said, I'd gladly switch to something that's smaller (FMOD is ~300k) and open-source. Super bonus points if it's fully managed so that I can compile / merge it with my .exe and not have to take extra care to get portability to other platforms...

(FMOD does portability too but you'd obviously need different binaries for different platforms)

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
0

This is not the most elegant solution but it's an easy solution. Send the stream from the web response to a file then play it using ffplay. It's cross platform and accepts most file formats without any configuration. Here is a github gist that shows a method to do that: https://gist.github.com/skittleson/095c0e9a4d56fbc53b80d22029e90c9b

I would like to figure out how to do this by streaming directly to ffplay verses a temp file.

UPDATE: It can be done with stream.

await Cli.Wrap("ffplay")
.WithStandardInputPipe(PipeSource.FromStream(stream))
.WithArguments($"-autoexit -nodisp -hide_banner -loglevel error -fs -")
.ExecuteAsync();
Spencer
  • 319
  • 5
  • 10