Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#1.0 C#2.0 C#3.0 C# C#4.0
Dear Readers,
 
I am using [NAudio] to record the input from my computer. I also play it while I've written the bytes to a Wave file, I hoped there was a way to play the byte[] without having to write it first but unfortunatly there is no way to do that. Anyways, here is my code, and I would hope that you have some suggestions to make my code better because right now it works, but it's really slow. >.>
 
public frmMain()
        {
            InitializeComponent();
        }
 
        private void frmMain_Load(object sender, EventArgs e)
        {
            // -- Create an input (Device/Stream)
            WaveIn _InputDevice = new WaveIn();
 
            // -- Change it to the device I want to record it from
            // - Using 1 as the default one.
            _InputDevice.DeviceNumber = 0;
 
            // -- Change the wave format.
            _InputDevice.WaveFormat = new WaveFormat(44100,2);
 
            // -- Create the event handlers.
            _InputDevice.DataAvailable += new EventHandler<WaveInEventArgs>(_InputDevice_DataAvailable);
            
            // -- Start recording.
            Thread _Thread = new Thread(_InputDevice.StartRecording);
            _Thread.Start();
        }
 
        WaveFileWriter _Writer;
        void _InputDevice_DataAvailable(object sender, WaveInEventArgs e)
        {
           // -- Get the correct path.
            string Path = Application.StartupPath + "\\" + DateTime.Now.TimeOfDay.Seconds + ".wav";
 
            // -- New Writer.
            _Writer = new WaveFileWriter(Path, new WaveFormat(24000, 16, 2));
 
            // -- Write all the bytes
            _Writer.Write(e.Buffer, 0, e.BytesRecorded);
 
            // -- Flush.
            _Writer.Flush();
 
            // -- Close.
            _Writer.Close();
 
            // -- Play it with a sound player.
            SoundPlayer _Player = new SoundPlayer();
            _Player.SoundLocation = Path;
            _Player.PlaySync();
 
            // -- Delete it.
            File.Delete(_Player.SoundLocation);
                
        }
Posted 12-Dec-12 4:41am
Yvar Birx2.4K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Alright, found the solution myself.
There was a method lying around the internet, took a while to find:
 
private void Play(byte[] p)
{
    try
    {
        WaveOut ou = new WaveOut();
        var s = new MemoryStream(p);
        RawSourceWaveStream r = new RawSourceWaveStream(s, new WaveFormat(44100, 2));
        ou.Init(r);
        ou.Play();
        ou.Stop();
        ou.Dispose();
        s.Dispose();
        r.Dispose();
    }
    catch { }
 
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Use the BufferedWaveProvider to do this. In the DataAvailable event handler, put the data you received into the BufferedWaveProvider. Then you can have a single instance of WaveOut playing from the buffered WaveProvider all the time.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 505
1 Gihan Liyanage 272
2 vikinghunter 176
3 ClimerChinna 172
4 Richard MacCutchan 150
0 Sergey Alexandrovich Kryukov 8,653
1 OriginalGriff 7,132
2 CPallini 2,598
3 Richard MacCutchan 2,060
4 Abhinav S 1,808


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100