Search code examples
c#naudiolive-streaming

How to live stream mp3 frames by frames in naudio


I am trying to live stream the audio in mp3 format while recording but i cannot achieve good streaming quality.

what i am doing is getting 10 seconds of PCM data from "WI_DataAvailable" and convert it to MP3 then send the frames in network. it produce little silent between the 10 sec of data.

I like to stream continues mp3 frames by frames while recording. is there any proper way?


Solution

  • Given that LameMP3FileWriter takes a Stream to write to, I would suggest implementing your own stream class and simply writing all the data that arrives in the Write method to UDP. Then you can pass this to the LameMP3FileWriter.

    Here's a bare-bones stream class that should get you started. You'll need to fill in the blanks for method Write, and possibly Flush. I imagine you can leave everything else as NotImplemented.

    public class UdpStream:Stream
    {
        public override int Read(byte[] buffer, int offset, int count)
        {
            //you'll definitely need to implement this...
            //write the buffer to UDP
        }
    
        public override void Flush()
        {
            //you might need to implement this
        }
    
        public override bool CanRead
        {
            get { return false; }
        }
    
        public override bool CanSeek
        {
            get { return false; }
        }
    
        public override bool CanWrite
        {
            get { return true; }
        }
    
        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }
    
        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }
    
    
        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotImplementedException();
        }
    
        public override long Length
        {
            get { throw new NotImplementedException(); }
        }
    
        public override long Position { 
            get{throw new NotImplementedException();} 
            set{throw new NotImplementedException();} 
        }
    }