我正在实现一个简单的录制、播放应用程序,它可以播放和录制 WAV 文件。
要求如下:
1) Windows 7、Visual Studio 2010(或 2012)、C#
2) 录制、播放带有规格信息的 Wav 文件:samplerate 22050、mono、bitspersample 16
3) 录制时可以显示微声级
使用 1) 和 2),我可以使用 lib (winmm.dll) 和 mciSendString(),如下所示
[DllImport("winmm.dll")]
private static extern long mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
// Open file to play
public void open(string file)
{
string command = "open \"" + file + "\" type waveaudio alias MyWav";
mciSendString(command, null, 0, 0);
}
// Start playing
public void play()
{
string command = "play MyWav";
mciSendString(command, null, 0, 0);
}
// Stop playing
public void stop()
{
string command = "stop MyWav";
mciSendString(command, null, 0, 0);
command = "close MyWav";
mciSendString(command, null, 0, 0);
}
// Start recording
public void record()
{
string command = "open new Type waveaudio Alias MyRec";
mciSendString(command, "", 0, 0);
command = "set capture time format ms bitspersample 16 channels 1 samplespersec 22050 alignment 4";
mciSendString(command, "", 0, 0);
command = "record MyRec";
mciSendString(command, "", 0, 0);
}
// Stop recording
public void stoprec()
{
string fileName = @"""D:\Rec1.wav""";
string command = "stop MyRec";
mciSendString(command, "", 0, 0);
command = @"save MyRec " + fileName;
Debug.WriteLine(command);
mciSendString(command, "", 0, 0);
command = "close MyRec";
mciSendString(command, "", 0, 0);
}
对于 3),我还想使用 mciSendString。我找到了以下命令(状态):
string command = "status MyRec level ";
long mci_ret = mciSendString(command, "", 0, 0);
但我不知道从上面的命令中获取音量,谁能告诉我方法?
参考:来自 mciSendString() 的状态命令可在 Microsoft 文档中找到:http:
//msdn.microsoft.com/en-us/library/windows/desktop/dd798683%28v=vs.85%29.aspx
它说:级别:返回当前 PCM 音频样本值。