Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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


C#
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();
        
        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);

    }
Posted
Updated 12-Nov-13 5:58am
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900