I use the part of code below to enable the microphone to record and then i save it and play it with 2 buttons. But if in savedialog i save my .wav in desktop folder everything is fine and it create the test.wav(example) but if i will try to save it in any other directory/path it doesn't do anything.
I suppose that it can't create or save the sound. But i don't know why.
My code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
//voice recorder
namespace recorder
{
public partial class Form1 : Form
{
[DllImport("winmm.dll")]
private static extern int mciSendString(string MciComando, string MciRetorno, int MciRetornoLeng, int CallBack);
string musica = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Visible = false; //label1.Text=recording
}
//starts recording what I say to mic
private void btnStart_Click(object sender, EventArgs e)
{
label1.Visible = true;
mciSendString("open new type waveaudio alias Som", null, 0, 0);
mciSendString("record Som", null, 0, 0);
}
//stops recording and shows a save form
//This is where the problem is.
/* I don't want the program to ask the user for location and file name and format to be
saved as. I just want the file to save in c:\ with the filename "recording1" with format
as .wav */
private void btnStopnSave_Click(object sender, EventArgs e)
{
label1.Visible = false;
mciSendString("pause Som", null, 0, 0);
SaveFileDialog save = new SaveFileDialog();
save.Filter = "WAVE|*.wav";
if (save.ShowDialog() == DialogResult.OK) // this is where it needs to be altered
{
mciSendString("save Som " + save.FileName,null,0, 0);
mciSendString("close Som", null, 0, 0);
}
}
//lets user to open a WAV file by browsing files
private void btnPlay_Click(object sender, EventArgs e)
{
if (musica == "")
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Wave|*.wav";
if (open.ShowDialog() == DialogResult.OK)
{
musica = open.FileName;
mciSendString("play " + musica, null, 0, 0);
}
}
}
}
}
I have found the code here : saving .wav files without showdialog in C#
Just don't open a file selection Box / SaveFileDialog.
Do this:
private void btnStopnSave_Click(object sender, EventArgs e)
{
label1.Visible = false;
mciSendString("pause Som", null, 0, 0);
string filename = "whatever";
mciSendString("save Som " + filename, null,0, 0);
mciSendString("close Som", null, 0, 0);
}