This project has moved and is read-only. For the latest updates, please go here.

NAudio can't play some mp3 streaming

Dec 8, 2012 at 4:53 AM

I wrote a software with NAudio demo that play steaming MP3 file. I found that NAudio can play most of MP3 file, but can't work with some MP3 file. I checked the sampling rate and bit rate of MP3 files, and there are not difference.

Dec 8, 2012 at 5:14 PM

what do you mean "can't work"? What error message do you get?

Dec 9, 2012 at 6:39 AM

1.can't work means there's no sound came out, but steaming was sending all the time.

2.There's no error message. If I restart the NAudio, then NAudio can play steaming of the same MP3 file. It's strange.

Dec 9, 2012 at 6:41 AM

This is my code:

 private void PlayMP3Stream(byte[] mp3Bytes)       

{           

using (MemoryStream responseStream = new MemoryStream(mp3Bytes))           

{               

var readFullyStream = new ReadFullyStream(responseStream);               

frame = Mp3Frame.LoadFromStream(readFullyStream);
                if (decompressor == null)               

{                   

NAudio.Wave.WaveFormat waveFormat = new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2, frame.FrameLength, frame.BitRate);                   

decompressor = new AcmMp3FrameDecompressor(waveFormat);                   

this.bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat);                   

this.bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(20); // allow us to get well ahead of ourselves                    //this.bufferedWaveProvider.BufferedDuration = 250;                }

                int decompressed = decompressor.DecompressFrame(frame, buffer, 0);               

bufferedWaveProvider.AddSamples(buffer, 0, decompressed);           

}       

}

Dec 9, 2012 at 6:44 AM

 private void udpReceive()       

{           

try           

{               

playMP3Server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);               

playIPEP = new IPEndPoint(CQCCRI.Common.Utility.GetLocalIPV4Address(), Settings.Default.DefaultMulticastPort);                playMP3Server.Bind(playIPEP);               

playMP3Server.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(playPoint.Address, CQCCRI.Common.Utility.GetLocalIPV4Address()));               

playMP3Server.ReceiveTimeout = Settings.Default.PlayReceiveTimeout;

                playbackState = StreamingPlaybackState.Buffering;
                while (true)               

{                   

byte[] data = new byte[1024 * 10];                   

int length = this.playMP3Server.Receive(data);                   

byte[] mp3Bytes = new byte[length];                   

Array.Copy(data, 0, mp3Bytes, 0, mp3Bytes.Length);                   

PlayMP3Stream(mp3Bytes);
              }           

}           

catch (Exception) {     }       

}

Dec 9, 2012 at 7:21 AM

I debug the code step by step in VS2012, when NAudio stop playing steaming, I got NAudio mMException in VS.

Dec 9, 2012 at 7:33 AM

NAudio.MmException: AcmNotPossible calling acmStreamConvert

Dec 9, 2012 at 7:57 AM

I suspect you have a change of sample rate / channels within the file

for each frame in your MP3 file, output to debug frame.SampleRate, and  frame.ChannelMode , and see what the values for the one that fails are

Dec 12, 2012 at 7:23 AM

I traced the details of some mp3 file. when software playing one file streaming, the information of output is wired, look like the frame of the file is changed.

SampleRate 44100 ChannelMode JointStereo BitRate 320000

在 NAudio.MmException 中第一次偶然出现的“NAudio.dll”类型的异常

SampleRate 44100 ChannelMode JointStereo BitRate 256000

在 NAudio.MmException 中第一次偶然出现的“NAudio.dll”类型的异常

SampleRate 44100 ChannelMode JointStereo BitRate 256000

在 NAudio.MmException 中第一次偶然出现的“NAudio.dll”类型的异常

SampleRate 44100 ChannelMode JointStereo BitRate 224000

在 NAudio.MmException 中第一次偶然出现的“NAudio.dll”类型的异常

SampleRate 44100 ChannelMode JointStereo BitRate 160000

Dec 12, 2012 at 7:27 AM

bitrate changing should not be a problem - that just means it is VBR. What is a problem is if samplerate or channelmode ever change during the file. If so you'd need to re-open your output device at the new sample rate and channel count and re-create the MP3 decoder. Alternatively you could resample to the desired output sample rate.

Mark

Dec 14, 2012 at 7:25 AM

Thank you