0
votes

There is such a task: Need to implement a program. The program has a text box and the ability to open text files and paste text from a file into this text box. Also, the program should record audio. For this, I use the NAudio library. When you press CTRL + 1, audio is recording. When you press CTRL + 2, stop record. Each file is recorded by record number. The number is incremented after each entry. I implemented the program correctly, I took the code from NAudio from the article about recording audio in C # through NAudio. There was a write code right there in the button event, but I implemented separate methods. As a result, Visual Studio produces a list of errors. Please help me figure it out. Form File Code:

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);
                }
            }
        }
    }
}
1
Could you please show the errors you get?Klaus Gütter

1 Answers

0
votes

You did not close the method StartRecording() with a }. Therefore all following methods are local to StartRecording and not visible outside.

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;
 }