如何在 C# WPF 中录制 WAV?
How to record WAV in c# WPF?
我可以让这段代码在控制台应用程序中运行。但我希望它能在 c# visual studio 2010 中的 Windows Forms 应用程序上工作。
这是我的代码:
public partial class MainWindow : Window
{
[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);
...
private void buttonRecordWAV_click(object sender, RoutedEventArgs e)
{
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
mciSendString("record recsound", "", 0, 0);
Console.WriteLine("recording, press Enter to stop and save ...");
Console.ReadLine();
if (System.Windows.Forms.MessageBox.Show("recording, press OK to stop recording and save", "Recording...", MessageBoxButtons.OK) == System.Windows.Forms.DialogResult.OK)
{
System.Windows.Forms.MessageBox.Show("you entered the if statement");
mciSendString("save recsound result.wav", "", 0, 0);
mciSendString("close recsound ", "", 0, 0);
}
}
但是,当我执行此操作时,我没有在 bin/Debug 文件夹中看到任何 'result.wav' 文件。当我在控制台应用程序上 运行 这段代码时,它可以工作,我可以在 bin/Debug 文件夹中看到 'result.wav',当我播放该文件时,我听到了我录制的内容。
任何人都知道如何在 C# Forms 应用程序中进行这项工作?
谢谢
您的示例代码中混合了奇怪的技术。当您在以表单作为前端的应用程序中使用 Console.ReadLine
时。您还使用 System.Windows.Forms.MessageBox
,它是 winforms class,而不是 WPF class,在这里您应该使用 System.Windows.MessageBox
.
我只是将相同的内容快速组合在一起,但是使用您示例中的片段,它对我有用。样本如下;标记:
<Grid>
<Button x:Name="buttonRecordWav" Click="buttonRecordWav_Click">Start Recording</Button>
</Grid>
后面的代码中:
public enum MCIErrors
{
NO_ERROR = 0,
MCIERR_BASE = 256,
MCIERR_INVALID_DEVICE_ID = 257,
MCIERR_UNRECOGNIZED_KEYWORD = 259,
MCIERR_UNRECOGNIZED_COMMAND = 261,
MCIERR_HARDWARE = 262,
MCIERR_INVALID_DEVICE_NAME = 263,
MCIERR_OUT_OF_MEMORY = 264,
MCIERR_DEVICE_OPEN = 265,
MCIERR_CANNOT_LOAD_DRIVER = 266,
MCIERR_MISSING_COMMAND_STRING = 267,
MCIERR_PARAM_OVERFLOW = 268,
MCIERR_MISSING_STRING_ARGUMENT = 269,
MCIERR_BAD_INTEGER = 270,
MCIERR_PARSER_INTERNAL = 271,
MCIERR_DRIVER_INTERNAL = 272,
MCIERR_MISSING_PARAMETER = 273,
MCIERR_UNSUPPORTED_FUNCTION = 274,
MCIERR_FILE_NOT_FOUND = 275,
MCIERR_DEVICE_NOT_READY = 276,
MCIERR_INTERNAL = 277,
MCIERR_DRIVER = 278,
MCIERR_CANNOT_USE_ALL = 279,
MCIERR_MULTIPLE = 280,
MCIERR_EXTENSION_NOT_FOUND = 281,
MCIERR_OUTOFRANGE = 282,
MCIERR_FLAGS_NOT_COMPATIBLE = 283,
MCIERR_FILE_NOT_SAVED = 286,
MCIERR_DEVICE_TYPE_REQUIRED = 287,
MCIERR_DEVICE_LOCKED = 288,
MCIERR_DUPLICATE_ALIAS = 289,
MCIERR_BAD_CONSTANT = 290,
MCIERR_MUST_USE_SHAREABLE = 291,
MCIERR_MISSING_DEVICE_NAME = 292,
MCIERR_BAD_TIME_FORMAT = 293,
MCIERR_NO_CLOSING_QUOTE = 294,
MCIERR_DUPLICATE_FLAGS = 295,
MCIERR_INVALID_FILE = 296,
MCIERR_NULL_PARAMETER_BLOCK = 297,
MCIERR_UNNAMED_RESOURCE = 298,
MCIERR_NEW_REQUIRES_ALIAS = 299,
MCIERR_NOTIFY_ON_AUTO_OPEN = 300,
MCIERR_NO_ELEMENT_ALLOWED = 301,
MCIERR_NONAPPLICABLE_FUNCTION = 302,
MCIERR_ILLEGAL_FOR_AUTO_OPEN = 303,
MCIERR_FILENAME_REQUIRED = 304,
MCIERR_EXTRA_CHARACTERS = 305,
MCIERR_DEVICE_NOT_INSTALLED = 306,
MCIERR_GET_CD = 307,
MCIERR_SET_CD = 308,
MCIERR_SET_DRIVE = 309,
MCIERR_DEVICE_LENGTH = 310,
MCIERR_DEVICE_ORD_LENGTH = 311,
MCIERR_NO_INTEGER = 312,
MCIERR_WAVE_OUTPUTSINUSE = 320,
MCIERR_WAVE_SETOUTPUTINUSE = 321,
MCIERR_WAVE_INPUTSINUSE = 322,
MCIERR_WAVE_SETINPUTINUSE = 323,
MCIERR_WAVE_OUTPUTUNSPECIFIED = 324,
MCIERR_WAVE_INPUTUNSPECIFIED = 325,
MCIERR_WAVE_OUTPUTSUNSUITABLE = 326,
MCIERR_WAVE_SETOUTPUTUNSUITABLE = 327,
MCIERR_WAVE_INPUTSUNSUITABLE = 328,
MCIERR_WAVE_SETINPUTUNSUITABLE = 329,
MCIERR_SEQ_DIV_INCOMPATIBLE = 336,
MCIERR_SEQ_PORT_INUSE = 337,
MCIERR_SEQ_PORT_NONEXISTENT = 338,
MCIERR_SEQ_PORT_MAPNODEVICE = 339,
MCIERR_SEQ_PORT_MISCERROR = 340,
MCIERR_SEQ_TIMER = 341,
MCIERR_SEQ_PORTUNSPECIFIED = 342,
MCIERR_SEQ_NOMIDIPRESENT = 343,
MCIERR_NO_WINDOW = 346,
MCIERR_CREATEWINDOW = 347,
MCIERR_FILE_READ = 348,
MCIERR_FILE_WRITE = 349,
MCIERR_CUSTOM_DRIVER_BASE = 512
};
[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);
private bool _recording = false;
private void buttonRecordWav_Click(object sender, RoutedEventArgs e)
{
MCIErrors result;
if (!_recording)
{
result = (MCIErrors)mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
if (result != MCIErrors.NO_ERROR)
{
MessageBox.Show("Error code: " + result.ToString());
return;
}
result = (MCIErrors)mciSendString("record recsound", "", 0, 0);
if (result != MCIErrors.NO_ERROR)
{
MessageBox.Show("Error code: " + result.ToString());
return;
}
buttonRecordWav.Content = "Stop Recording";
_recording = true;
}
else
{
result = (MCIErrors)mciSendString("save recsound result.wav", "", 0, 0);
if (result != MCIErrors.NO_ERROR)
{
MessageBox.Show("Error code: " + result.ToString());
return;
}
result = (MCIErrors)mciSendString("close recsound ", "", 0, 0);
if (result != MCIErrors.NO_ERROR)
{
MessageBox.Show("Error code: " + result.ToString());
return;
}
buttonRecordWav.Content = "Start Recording";
_recording = false;
}
}
如果我 运行 那样,我就会按预期创建 wav 文件。此处有关 mciSendString 方法的文档:
无法对已接受的解决方案发表评论(无代表),但人们对已接受解决方案的问题是由于文件路径中的空格。要解决,只需将 完整路径 传递给 mciSendString 并 将其用引号 括起来,例如:
string fullPath = @"c:\path with spaces\result.wav";
result = (MCIErrors)mciSendString("save recsound \"" + fullPath + "\"", "", 0, 0);
我可以让这段代码在控制台应用程序中运行。但我希望它能在 c# visual studio 2010 中的 Windows Forms 应用程序上工作。 这是我的代码:
public partial class MainWindow : Window
{
[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);
...
private void buttonRecordWAV_click(object sender, RoutedEventArgs e)
{
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
mciSendString("record recsound", "", 0, 0);
Console.WriteLine("recording, press Enter to stop and save ...");
Console.ReadLine();
if (System.Windows.Forms.MessageBox.Show("recording, press OK to stop recording and save", "Recording...", MessageBoxButtons.OK) == System.Windows.Forms.DialogResult.OK)
{
System.Windows.Forms.MessageBox.Show("you entered the if statement");
mciSendString("save recsound result.wav", "", 0, 0);
mciSendString("close recsound ", "", 0, 0);
}
}
但是,当我执行此操作时,我没有在 bin/Debug 文件夹中看到任何 'result.wav' 文件。当我在控制台应用程序上 运行 这段代码时,它可以工作,我可以在 bin/Debug 文件夹中看到 'result.wav',当我播放该文件时,我听到了我录制的内容。 任何人都知道如何在 C# Forms 应用程序中进行这项工作? 谢谢
您的示例代码中混合了奇怪的技术。当您在以表单作为前端的应用程序中使用 Console.ReadLine
时。您还使用 System.Windows.Forms.MessageBox
,它是 winforms class,而不是 WPF class,在这里您应该使用 System.Windows.MessageBox
.
我只是将相同的内容快速组合在一起,但是使用您示例中的片段,它对我有用。样本如下;标记:
<Grid>
<Button x:Name="buttonRecordWav" Click="buttonRecordWav_Click">Start Recording</Button>
</Grid>
后面的代码中:
public enum MCIErrors
{
NO_ERROR = 0,
MCIERR_BASE = 256,
MCIERR_INVALID_DEVICE_ID = 257,
MCIERR_UNRECOGNIZED_KEYWORD = 259,
MCIERR_UNRECOGNIZED_COMMAND = 261,
MCIERR_HARDWARE = 262,
MCIERR_INVALID_DEVICE_NAME = 263,
MCIERR_OUT_OF_MEMORY = 264,
MCIERR_DEVICE_OPEN = 265,
MCIERR_CANNOT_LOAD_DRIVER = 266,
MCIERR_MISSING_COMMAND_STRING = 267,
MCIERR_PARAM_OVERFLOW = 268,
MCIERR_MISSING_STRING_ARGUMENT = 269,
MCIERR_BAD_INTEGER = 270,
MCIERR_PARSER_INTERNAL = 271,
MCIERR_DRIVER_INTERNAL = 272,
MCIERR_MISSING_PARAMETER = 273,
MCIERR_UNSUPPORTED_FUNCTION = 274,
MCIERR_FILE_NOT_FOUND = 275,
MCIERR_DEVICE_NOT_READY = 276,
MCIERR_INTERNAL = 277,
MCIERR_DRIVER = 278,
MCIERR_CANNOT_USE_ALL = 279,
MCIERR_MULTIPLE = 280,
MCIERR_EXTENSION_NOT_FOUND = 281,
MCIERR_OUTOFRANGE = 282,
MCIERR_FLAGS_NOT_COMPATIBLE = 283,
MCIERR_FILE_NOT_SAVED = 286,
MCIERR_DEVICE_TYPE_REQUIRED = 287,
MCIERR_DEVICE_LOCKED = 288,
MCIERR_DUPLICATE_ALIAS = 289,
MCIERR_BAD_CONSTANT = 290,
MCIERR_MUST_USE_SHAREABLE = 291,
MCIERR_MISSING_DEVICE_NAME = 292,
MCIERR_BAD_TIME_FORMAT = 293,
MCIERR_NO_CLOSING_QUOTE = 294,
MCIERR_DUPLICATE_FLAGS = 295,
MCIERR_INVALID_FILE = 296,
MCIERR_NULL_PARAMETER_BLOCK = 297,
MCIERR_UNNAMED_RESOURCE = 298,
MCIERR_NEW_REQUIRES_ALIAS = 299,
MCIERR_NOTIFY_ON_AUTO_OPEN = 300,
MCIERR_NO_ELEMENT_ALLOWED = 301,
MCIERR_NONAPPLICABLE_FUNCTION = 302,
MCIERR_ILLEGAL_FOR_AUTO_OPEN = 303,
MCIERR_FILENAME_REQUIRED = 304,
MCIERR_EXTRA_CHARACTERS = 305,
MCIERR_DEVICE_NOT_INSTALLED = 306,
MCIERR_GET_CD = 307,
MCIERR_SET_CD = 308,
MCIERR_SET_DRIVE = 309,
MCIERR_DEVICE_LENGTH = 310,
MCIERR_DEVICE_ORD_LENGTH = 311,
MCIERR_NO_INTEGER = 312,
MCIERR_WAVE_OUTPUTSINUSE = 320,
MCIERR_WAVE_SETOUTPUTINUSE = 321,
MCIERR_WAVE_INPUTSINUSE = 322,
MCIERR_WAVE_SETINPUTINUSE = 323,
MCIERR_WAVE_OUTPUTUNSPECIFIED = 324,
MCIERR_WAVE_INPUTUNSPECIFIED = 325,
MCIERR_WAVE_OUTPUTSUNSUITABLE = 326,
MCIERR_WAVE_SETOUTPUTUNSUITABLE = 327,
MCIERR_WAVE_INPUTSUNSUITABLE = 328,
MCIERR_WAVE_SETINPUTUNSUITABLE = 329,
MCIERR_SEQ_DIV_INCOMPATIBLE = 336,
MCIERR_SEQ_PORT_INUSE = 337,
MCIERR_SEQ_PORT_NONEXISTENT = 338,
MCIERR_SEQ_PORT_MAPNODEVICE = 339,
MCIERR_SEQ_PORT_MISCERROR = 340,
MCIERR_SEQ_TIMER = 341,
MCIERR_SEQ_PORTUNSPECIFIED = 342,
MCIERR_SEQ_NOMIDIPRESENT = 343,
MCIERR_NO_WINDOW = 346,
MCIERR_CREATEWINDOW = 347,
MCIERR_FILE_READ = 348,
MCIERR_FILE_WRITE = 349,
MCIERR_CUSTOM_DRIVER_BASE = 512
};
[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);
private bool _recording = false;
private void buttonRecordWav_Click(object sender, RoutedEventArgs e)
{
MCIErrors result;
if (!_recording)
{
result = (MCIErrors)mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
if (result != MCIErrors.NO_ERROR)
{
MessageBox.Show("Error code: " + result.ToString());
return;
}
result = (MCIErrors)mciSendString("record recsound", "", 0, 0);
if (result != MCIErrors.NO_ERROR)
{
MessageBox.Show("Error code: " + result.ToString());
return;
}
buttonRecordWav.Content = "Stop Recording";
_recording = true;
}
else
{
result = (MCIErrors)mciSendString("save recsound result.wav", "", 0, 0);
if (result != MCIErrors.NO_ERROR)
{
MessageBox.Show("Error code: " + result.ToString());
return;
}
result = (MCIErrors)mciSendString("close recsound ", "", 0, 0);
if (result != MCIErrors.NO_ERROR)
{
MessageBox.Show("Error code: " + result.ToString());
return;
}
buttonRecordWav.Content = "Start Recording";
_recording = false;
}
}
如果我 运行 那样,我就会按预期创建 wav 文件。此处有关 mciSendString 方法的文档:
无法对已接受的解决方案发表评论(无代表),但人们对已接受解决方案的问题是由于文件路径中的空格。要解决,只需将 完整路径 传递给 mciSendString 并 将其用引号 括起来,例如:
string fullPath = @"c:\path with spaces\result.wav";
result = (MCIErrors)mciSendString("save recsound \"" + fullPath + "\"", "", 0, 0);