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

How to call stop method while waveIn_DataAvailable method is in running state in console application

Nov 12, 2013 at 9:14 AM
Edited Nov 12, 2013 at 12:57 PM
Hi
I am capturing system speaker audio with the help of WasapiLoopbackCapture(). I put this whole code in a console application and invoke this from cmd. Now the problem is when is execute this I am unable to call stop method as it is stuck in recording. what I want first I call my exe like this "captureAudio.exe start" and its will exit from exe and when I want to stop then I call "captureAudio.exe stop" . is it possible here is my code.
I use thread to invoke but it still stuck...
Any help. Thanks

static void Main(string[] args)
    {


        string[] argReceived = args[0].Split('-');
        for (int i = 0; i < argReceived.Length; i++)
        {
            Debug.WriteLine(i + " : = " + argReceived[i]);
        }
        Thread captureAudio;
       if (argReceived[0] == "start")
        {

            captureAudio = new Thread(new ThreadStart(wavtomp3));
            captureAudio.Start();

        }




    }

static void wavtomp3()
    {

        // Start recording from loopback
        waveIn = new WasapiLoopbackCapture();
       // MessageBox.Show("Sample Rate=" + waveIn.WaveFormat.SampleRate.ToString());
        string fname = DateTime.Now.Ticks.ToString();
        fname = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments  + "\\23295\\" + fname + ".mp3";
        wri = new LameMP3FileWriter(fname, waveIn.WaveFormat, 44);

        waveIn.DataAvailable += waveIn_DataAvailable;
        waveIn.RecordingStopped += waveIn_RecordingStopped;
        waveIn.StartRecording();

    }

    static void waveIn_RecordingStopped(object sender, StoppedEventArgs e)
    {
        if (waveIn != null) // working around problem with double raising of RecordingStopped
        {
            waveIn.Dispose();
            waveIn = null;
        }
        if (wri != null)
        {
            wri.Close();
            wri = null;
        }

    }

    static void waveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
        if (wri != null)

            if (e.BytesRecorded == 0)
            {
                wri.Write(e.Buffer, 0, 12800);
                Debug.WriteLine("Adding Silence To File No Sound is playing");
            }
            else if (e.BytesRecorded > 0)
            {
                wri.Write(e.Buffer, 0, e.BytesRecorded);
                Debug.WriteLine("Sound Playing From Sound Card and recording");
            }

        Debug.WriteLine("bytes record=" + e.BytesRecorded);

    }
Nov 12, 2013 at 5:45 PM
you can't launch a second instance of your app to stop the first instance recording. A simple solution might simply be to have a Console.ReadKey in there, so the user can press a key to stop recording