0
votes

For the test purpose I have implemented the c# code just to play a single segment of a buffer (by doing it in a while loop I can read the whole data coming from socket and play) I used this three versions of nAudio (v1.2, v1.3, v1.7) The three versions give different errors in that context, I have listed them bellow (Which I'm also confused why it happens). I have attached my source code with this.

when using NAudio 1.3

  • FormatException was unhandled.
  • discription -Not a recognised MP3 block
  • error line :- using (WaveStream blockAlignedStream = new BlockAlignReductionStream( WaveFormatConversionStream.CreatePcmStream( new Mp3FileReader(ms))))

when using NAudio 1.6

  • InvaliedOperationException was unhandled
  • discription -got a frame at sample rate 16000,in an mp3 with sample rate 48000.Mp3FileReader does not support sample rate changes.
  • error line -same line

when usng NAudio 1.7

  • NullReferenceException was unhandled
  • discription -Object reference not set to an instance of an object.
  • eror line -same line

I used 1.7 instead 1.6 as it supports sample rate changes (mentioned in the sources). Then I get the above error. Each time I get the error line the same line. As I'm novice to C# and .net platforms I need your help in correcting this. Thank you and looking forward a response.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using NAudio.Wave;
using System.IO;
using System.Threading;

namespace audioTest2
{
    class Program
    {
        public static void Main()
        {

                while (true)
                {
                    Console.WriteLine("Waiting for broadcast");


                    UdpClient listener = new UdpClient(5000);
                    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 5000);
                    byte[] buffer = listener.Receive(ref groupEP);
                    using (Stream ms = new MemoryStream())
                    {

                        buffer = listener.Receive(ref groupEP);
                        Console.WriteLine("read : " + buffer.Length);
                        ms.Write(buffer,0,buffer.Length);

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

                    }
                }
            }

        }

}
1

1 Answers

0
votes

I would strongly recommend looking at the MP3 streaming demo that comes with the NAudio source code (in the NAudioDemo project). You don't need the WaveFormatConversionStream or the BlockAlignReduction stream when reading MP3 files, and you should avoid function callbacks - use WaveOutEvent instead. Also, the Mp3FileReader class is designed for entire MP3 files that you can reposition in. The approach you need is to parse MP3 frames as they arrive, decode them individually, and buffer the audio in some way (e.g. use the BufferedWaveProvider).