title: Play MP3 using SoundPlayer after conversion to WAV using NAudio
body: <p>I want to play MP3 file downloaded from the web using NET provided <a href="http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx" rel="nofollow">System.Media.SoundPlayer</a> mechanism. As it works with WAV formats, it requires the support of e.g. <a href="http://naudio.codeplex.com/" rel="nofollow">NAudio</a> library - I need to convert MP3 to WAV. </p>
  
  <p>I want to do all operations in memory, as I need it to be so called fast, but I have problems. Below I've shown code which works as expected, but it cooperates with files. Instead I need to make it working using memory operations only.</p>
  
  <p>(1) works, but involves disk operations:</p>
  
  <pre><code>public void Speak(Uri mp3FileUri)
  {
      using (var client = new WebClient())
      {
          using (var networkStream = client.OpenRead(mp3FileUri))
          {
              if (networkStream != null)
              {
                  var temp = Path.GetTempPath();
                  var mp3File = Path.Combine(temp, "file.mp3");
                  var wavFile = Path.Combine(temp, "file.wav");
                  using (var fileStream = File.Create(mp3File))
                  {
                      networkStream.CopyTo(fileStream);
                  }
                  using (var reader = new Mp3FileReader(mp3File))
                  {
                      WaveFileWriter.CreateWaveFile(wavFile, reader);
                  }                        
                  using(var player = new SoundPlayer(wavFile))
                  {
                      player.Play();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>(2) doesn't work - no exception is thrown, but nothing is played:</p>
  
  <pre><code>public void Speak(Uri mp3FileUri)
  {
      using (var client = new WebClient())
      {
          using (var networkStream = client.OpenRead(mp3FileUri))
          {
              if (networkStream != null)
              {
                  var memStream = new MemoryStream();
                  networkStream.CopyTo(memStream);
                  memStream.Position = 0;
                  using (var reader = new Mp3FileReader(memStream))
                  {
                      var outStream = new MemoryStream();
                      using (var writer = new WaveFileWriter(outStream, reader.WaveFormat))
                      {
                          var num = 0L;
                          var buffer = new byte[reader.WaveFormat.AverageBytesPerSecond * 4];
                          while (true)
                          {
                              var count = reader.Read(buffer, 0, buffer.Length);
                              if (count != 0)
                              {
                                  num += count;
                                  if (num &lt;= int.MaxValue)
                                      writer.Write(buffer, 0, count);
                                  else
                                      throw new InvalidOperationException("Too large file or endless stream.");
                              }
                              else
                                  break;
                          }
                          writer.Flush();
                          outStream.Position = 0;
                          using(var player = new SoundPlayer(outStream))
                          {
                              player.Play(); /* why silence ? */
                          }
                      }                            
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>How can it be done and what is wrong with the second code sample ?</p>