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

Frame always is null

Dec 25, 2013 at 5:09 PM
Edited Dec 26, 2013 at 7:56 AM
I am working on a proiect using NAudio and C#. What I want to do is to be able to broadcast an mp3 to a server receive and play it on the client side . I have 3 application 1 that brodcast the mp3 file , the server and the client.

On the brodcast application I have something like this:
Mp3FileReader audioFile = new Mp3FileReader("C:/test.mp3");
audioFile.Position = 0;

int transfered = 0;
byte[] buffer = new byte[4096];

while (transfered <= audioFile.Length) {
    transfered += audioFile.Read(buffer, 0, buffer.Length);
    SendAudio(buffer); //is a method that send data over UDP network - no worries about it
    Array.Clear(buffer, 0, buffer.Length);
    Thread.Sleep(1000); //this to not make lag on the server
}
And on the client I have copied the code from MP3StreamingDemo with a bit of modification:
MemoryStream audioStream = new MemoryStream();
public void FillMemoryStream(byte[] data) {
    audioStream.Write(data, 0, data.Length);
}

private void StreamMp3(object state) {
    fullyDownloaded = false;

    var buffer = new byte[16384 * 4]; // needs to be big enough to hold a decompressed frame
    audioStream.Position = 0;

    IMp3FrameDecompressor decompressor = null;
    try {
        using (audioStream) {
            var readFullyStream = new ReadFullyStream(audioStream);
            do {
                if (IsBufferNearlyFull) {
                    Debug.WriteLine("Buffer getting full, taking a break");
                    Thread.Sleep(500);
                } else {
                     Mp3Frame frame;
                     try {
                         frame = Mp3Frame.LoadFromStream(readFullyStream);
                      } catch (EndOfStreamException) {
                                fullyDownloaded = true;
                                // reached the end of the MP3 file / stream
                                break;
                      } catch (WebException) {
                                // probably we have aborted download from the GUI thread
                                break;
                      }
                      if (decompressor == null) {
                            decompressor = CreateFrameDecompressor(frame);
                            bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat);
                            bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(20);of ourselves;
                      }
                        int decompressed = decompressor.DecompressFrame(frame, buffer, 0);                                                         
                        bufferedWaveProvider.AddSamples(buffer, 0, decompressed);
                       }

                    } while (playbackState != StreamingPlaybackState.Stopped);
                    Debug.WriteLine("Exiting");
                    decompressor.Dispose();
                }
            } finally {
                if (decompressor != null) {
                    decompressor.Dispose();
                }
            }
        }
The mp3Frame is always null what should I do ?
Dec 26, 2013 at 12:01 AM
Edited Dec 26, 2013 at 7:56 AM
Even if I try to send data frame by frame I get null frame on the client side.
    Mp3Frame frame = null;
    do {
        frame = audioFile.ReadNextFrame();
        if (frame == null) {
               //SendEOF();
               break;
         }
         SendAudio(frame.RawData);
         Thread.Sleep(1000);
    } while (frame != null || playing != false);
Dec 26, 2013 at 10:00 AM
null frame means you got to the end.
Dec 28, 2013 at 5:30 PM
Yes . I know but even I provide a lot of information I get null frame. Can you look after my code posted here codereview and say what is wrong ?

If someone worked with ENet C# library and NAudio or he want to learn along with me, contact me at g30.cristian@gmail.com.