2013-02-11 133 views
1

我正在實現一個簡單的記錄,播放可以播放和錄製WAV文件的應用程序。
的要求如下:在Windows 7中錄製時獲取麥克風聲音級別

1)Windows 7中的Visual Studio 2010(或2012),C#
2)錄製,播放與規範信息的Wav文件:採樣率22050,單聲道,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),I也希望使用mciSendString。我發現以下命令(狀態):

string command = "status MyRec level "; 
long mci_ret = mciSendString(command, "", 0, 0); 

但我不知道從上面的命令獲取音量,任何人都可以告訴我的方式?

參考:從mciSendString狀態命令()在微軟的文檔中發現: http://msdn.microsoft.com/en-us/library/windows/desktop/dd798683%28v=vs.85%29.aspx
它說: 級別:返回當前PCM音頻採樣值。

+0

有在CodeProject上[Vista的核心音頻API主音量控制(HTTP一些偉大的articales://www.codeproject。 com/Articles/18520/Vista-Core-Audio-API-Master-Volume-Control?fid = 410669&fr = 126#xx0xx) – sees 2013-02-12 11:06:48

+0

或[在Visual C++中更改主卷](http://www.codeproject.com/Tips/233484/Change-Master-Volume-in-Visual-Cplusplus) – sees 2013-02-12 11:08:18

回答

0

我通過打開兩個設備(一個用於錄製/播放(track1)和一個獲取關卡(waveLevel)來監視關卡。下面是在foxpro。

* OPEN WAVE DEVICE FOR LEVEL READINGS 
= mciSendString("open new type waveaudio alias waveLevel", '0&', 0, 0) 

* START RECORDING 
= mciSendString("open new type waveaudio alias track1", '0&', 0, 0) 

然後拿到TRACK1的水平我用的是waveLevel設備

= mciSendString("status waveLevel level", @cBuff, LEN(cBuff) ,0) 
+0

感謝您的建議。我最終使用NAudio來完成我的任務。閱讀所有代碼並找出正確的方法。對於Windows 7和Windows 8都可以(在專業版,企業版,家庭高級版,等等版本中進行檢查) – sees 2013-05-22 04:20:48