使用winmm.dll在c#中录制音频

时间:2015-08-01 12:25:50

标签: c# audio-recording winmm

我正在用C#开发一个应用程序,我正在尝试使用winmm.dll来录制声音。几天前我能够录制音频,但现在音频文件没有生成。也不会产生错误。我调试了程序,但无法找到我错的地方。

这是我的代码:

    bool rec_strt = false;
    static int incre = 1;
    //string save_path = "E:\\mic"+incre+".wav";
    string save_path = Path.Combine(Environment.CurrentDirectory, @"Sent_files\", incre + ".wav");

    [DllImport("winmm.dll", EntryPoint = "mciSendStringA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
    private static extern int record(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
    private void p_audio_Click(object sender, EventArgs e)
    {
        if (rec_strt == false)
        {

            try
            {//getting both  i.e values of oo and op 0 ,which meanse no error
                int oo = record("open new Type waveaudio Alias recsound", "", 0, 0);
                int op = record("record recsound", "", 0, 0);
                string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                if (op != 0)
                {
                    MessageBox.Show("Please attach the recording device first...", "Error message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    toolTip1.SetToolTip(p_audio, "Click to stop recording");
                    p_audio.BackgroundImage = WindowsFormsApplication1.Properties.Resources.voice_recording;
                    timer1.Enabled = true;
                    timer1.Start();
                  //  toolTip2.Show(timer1.ToString(), p_audio);
                    rec_strt = true;
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("Error occured: " + er.Message, "Error message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        else
        {
            toolTip1.SetToolTip(p_audio, "Click to start recording");
            p_audio.BackgroundImage = WindowsFormsApplication1.Properties.Resources.voice;
            MessageBox.Show("Do you want to send this message to " + l_cnm.Text, "Confirmation message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            timer1.Stop();
            timer1.Enabled = false;
            toolTip2.Hide(p_audio);
            try
            {
                record("save recsound" + Path.Combine(Environment.CurrentDirectory, @"Sent_files\", incre + ".wav"), "", 0, 0);
                record("close recsound", "", 0, 0);
                try
                {
                    NameValueCollection nvc = new NameValueCollection();
                    nvc.Add("sender", my_id);
                    nvc.Add("receiver", fr_id);
                    nvc.Add("file", "Upload");
                    string contype = GetContentType(Path.GetExtension(save_path));
                    HttpUploadFile("http://chat.bobl.us/msgfile.php", save_path, "file", contype, nvc);
                    string fname = save_path.Substring(save_path.LastIndexOf("\\") + 1);
                    i1 = save_path;
                    add_label(true, "aud_" + fr_id + "_" + fname, DateTime.Now);
                    //p_cpic.Image = Image.FromFile(i);
                }
                catch (Exception ex)
                { }
            }
            catch (Exception er)
            {
                MessageBox.Show("Error occured: ", "Error message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            rec_strt = false;
            incre++;
        }
    }

0 个答案:

没有答案