Answer the question
In order to leave comments, you need to log in
How to solve problems with NAudio?
Hello.
There is such task: It is
necessary to implement the program. The program has a text field and the ability to open text files and paste text from the file into this text field.
Also, the program should record audio. For this I use the NAudio library.
Pressing CTRL+1 plays the audio. When you press CTRL+2, stop playback.
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 an article about recording audio in C # through NAudio. There was a recording code immediately 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.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio;
using NAudio.CoreAudioApi;
using NAudio.FileFormats;
using NAudio.Wave;
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()
{
recordFile = Convert.ToString(numberRecords) + ".wav";
try
{
waveIn = new WaveIn();
waveIn.DeviceNumber = 0;
waveIn.DataAvailable += waveIn_DataAvailable;
waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
waveIn.WaveFormat = new WaveFormat(8000, 1);
writer = new WaveFileWriter(recordFile, waveIn.WaveFormat);
waveIn.StartRecording();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}
void StopRecording()
{
waveIn.StopRecording();
numberRecords += 1;
}
private 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);
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question