Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Windows计时器已过事件的音频录制问题_C#_Audio_Timer_Voice Recording_Elapsed - Fatal编程技术网

C# 使用Windows计时器已过事件的音频录制问题

C# 使用Windows计时器已过事件的音频录制问题,c#,audio,timer,voice-recording,elapsed,C#,Audio,Timer,Voice Recording,Elapsed,我正在按时间间隔录制windows应用程序中的语音。我制作了一个类来启动和停止语音记录,并在我的表单上调用它的函数 课程安排如下 class VoiceRecording { [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern int mciSe

我正在按时间间隔录制windows应用程序中的语音。我制作了一个类来启动和停止语音记录,并在我的表单上调用它的函数

课程安排如下

class VoiceRecording {
    [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
    public VoiceRecording() {

    }

    public void StartRecording() {
        mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
        mciSendString("record recsound", "", 0, 0);
    }

    public void StopRecording(int FileNameCounter) {
        mciSendString(String.Format("save recsound {0}", @"E:\WAVFiles\" + FileNameCounter + ".wav"), "", 0, 0);
        mciSendString("close recsound ", "", 0, 0);
        Computer c = new Computer();
        c.Audio.Stop();
    }
}
现在,当我在按钮上调用这些函数时,单击事件

    int FileNameCounter = 1;
    private void btnStart_Click(object sender, EventArgs e) {
        VR = new VoiceRecording();
        VR.StartRecording();
    }

    private void btnStop_Click(object sender, EventArgs e) {
        VR.StopRecording(FileNameCounter++);
        VR = null;
    }
一切都很顺利,无论我点击按钮的速度有多慢,代码总是创建编号文件

我将代码放入循环中,就像

for (int i = 0; i < 10; i++) {
   VR = new VoiceRecording();
   VR.StartRecording();
   VR.StopRecording(FileNameCounter++);
   VR = null;
}
现在的问题是,当代码在TimerEvent内执行时,它正在创建文件,但也缺少一些文件。

例如
循环创建:1.wav、2.wav、3.wav、4.wav、5.wav、6.wav
计时器创建:1.wav、2.wav、4.wav、7.wav、8.wav、13.wav

我已经调试了代码,每次都在执行每个语句,但有时没有创建文件


任何帮助都将不胜感激:)

正如汉斯·帕桑所说


System.Timers.Timer是一门很难的课程,为不可诊断的故障创造了各种机会。重新进入总是一种风险,它吞下所有例外的习惯尤其麻烦。不要用它,你不需要它。改用常规的Winforms计时器

使用Winforms定时器解决了这个问题。现在,已按我的要求创建编号的文件。:)


谢谢你Hans Passant

没有这方面的经验,但是在你的语音录制课上,你向一些底层对象发送消息而没有检查是否成功-我想从寻找获取状态/检索错误的方法开始。System.Timers.Timer是一门很难的课,为无法诊断的失败创造各种机会。重新进入总是一种风险,它吞下所有例外的习惯尤其麻烦。不要用它,你不需要它。改用常规的Winforms计时器。
System.Timers.Timer t = new System.Timers.Timer();
t.Elapsed += new ElapsedEventHandler(TimerEvent);
t.Interval = 10000;
t.Start();

private bool RecordingStarted = false;
private void TimerEvent(object sender, ElapsedEventArgs e) {
    if (RecordingStarted) {
        VR.StopRecording(FileNameCounter++);
        VR = null;
        RecordingStarted = false;
    } else {
        VR = new VoiceRecording();
        VR.StartRecording();
        RecordingStarted = true;
    }
}