有这样一个任务: 需要执行一个程序。该程序具有一个文本框,并且可以打开文本文件并将文件中的文本粘贴到此文本框中。 此外,该程序应录制音频。为此,我使用了NAudio库。 当您按CTRL + 1时,正在录制音频。当您按CTRL + 2时,停止记录。 每个文件均按记录号记录。每次输入后,数字都会增加。 我正确实现了该程序,我从NAudio中获取了有关通过NAudio在C#中记录音频的文章中的代码。按钮事件中在那里有一个编写代码,但是我实现了单独的方法。 结果,Visual Studio生成错误列表。 请帮我弄清楚。 表单文件代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.Wave;
using NAudio.FileFormats;
using NAudio.CoreAudioApi;
using NAudio;
namespace RecordBooks
{
public partial class Form1 : Form
{
WaveIn waveIn;
WaveFileWriter writer;
string recordFile = "";
int numberRecords = 0;
public Form1()
{
InitializeComponent();
KeyPreview = true;
KeyDown += Form1_KeyDown;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.D1)
{
StartRecording();
}
else if (e.Control && e.KeyCode == Keys.D2)
{
if (waveIn != null)
{
StopRecording();
}
}
}
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new EventHandler<WaveInEventArgs>(waveIn_DataAvailable), sender, e);
}
else
{
writer.WriteData(e.Buffer, 0, e.BytesRecorded);
}
}
void StartRecording()
{
try
{
waveIn = new WaveIn();
waveIn.DeviceNumber = 0;
waveIn.DataAvailable += waveIn_DataAvailable;
waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
waveIn.WaveFormat = new WaveFormat(8000, 1);
recordFile = numberRecords + ".wav";
writer = new WaveFileWriter(recordFile);
waveIn.StartRecording();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
void StopRecording()
{
waveIn.StopRecording();
numberRecords += 1;
}
void waveIn_RecordingStopped(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new EventHandler(waveIn_RecordingStopped), sender, e);
}
else
{
waveIn.Dispose();
waveIn = null;
writer.Close();
writer = null;
}
}
void menuNew_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
numberRecords = 0;
}
void menuExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
void menuOpen_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName.Length > 0)
{
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
numberRecords = 0;
}
}
void menuSave_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
}
}
}
}
}
答案 0 :(得分:0)
您没有使用}
关闭方法StartRecording()。因此,以下所有方法对于StartRecording都是本地的,在外部不可见。
void StartRecording()
{
try
{
waveIn = new WaveIn();
waveIn.DeviceNumber = 0;
waveIn.DataAvailable += waveIn_DataAvailable;
waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
waveIn.WaveFormat = new WaveFormat(8000, 1);
recordFile = numberRecords + ".wav";
writer = new WaveFileWriter(recordFile);
waveIn.StartRecording();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} // <<<-- this was missing
void StopRecording()
{
waveIn.StopRecording();
numberRecords += 1;
}