Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9215341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:11:00+00:00 2026-06-18T02:11:00+00:00

I want to play MP3 file downloaded from the web using NET provided System.Media.SoundPlayer

  • 0

I want to play MP3 file downloaded from the web using NET provided System.Media.SoundPlayer mechanism. As it works with WAV formats, it requires the support of e.g. NAudio library – I need to convert MP3 to WAV.

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.

(1) works, but involves disk operations:

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

(2) doesn’t work – no exception is thrown, but nothing is played:

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 <= 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 ? */
                        }
                    }                            
                }
            }
        }
    }
}

How can it be done and what is wrong with the second code sample ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:11:01+00:00Added an answer on June 18, 2026 at 2:11 am

    Based on Lee Harrison answer, I’ve created alternative code which is better.

    (3) plays directly MP3 file from WEB without conversion to WAV and without usage of disk operations:

    public void Speak(Uri mp3FileUri)
    {
        using (var client = new WebClient())
        {
            using (var networkStream = client.OpenRead(mp3FileUri))
            {
                if (networkStream != null)
                {
                    using (var memStream = new MemoryStream())
                    {
                        networkStream.CopyTo(memStream);
                        memStream.Position = 0;                            
                        using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
                        {
                            var waveEvent = new ManualResetEvent(false);
                            waveOut.PlaybackStopped += (sender, e) => waveEvent.Set();
                            waveEvent.Reset();
                            using (var rdr = new Mp3FileReader(memStream))
                            using (var waveStream = WaveFormatConversionStream.CreatePcmStream(rdr))
                            using (var baStream = new BlockAlignReductionStream(waveStream))
                            {
                                waveOut.Init(baStream);
                                waveOut.Play();
                                if (waveOut.PlaybackState != PlaybackState.Stopped)
                                { 
                                    waveEvent.WaitOne(); /* block thread for a while because I don't want async play back                                
                                                            (to be analogical as usage of SoundPlayer Play method) */
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    Nevertheless (despite this is quite good for me), I still don’t know what’s the problem with sample (2).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use media player control to play mp3 files in asp.net application. I want
I want to download a single .mp3 file from my site but when using
I am Using this code to play a .mp3 file from res/raw. The problem
I have an MP3 audio file that I want to play from my Android
mciSendStringi(,,,); I used the above function to play a mp3 file. Now I want
I have Application play some mp3 files from the internet and I want to
Hi I want to play a mp3 file via my server e.g. http://test.com/hi.mp3 At
I want to play mp3 file ONLY the right channel. -> left channel, set
I want to play mp3 file placed in resources as code is given below,
How can I open windows media player and play a mp3 file via a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.