现在要实现的是录音功能,可以录音,保存到wav文件到中。
唯一就是实现不了,实时显示声音强度。有用过NAudio的朋友吗? 用这个实现也行。录音过程有一个回调事件,waveInStream_DataAvailable()
里面buffer, byte数组,就是把这些数据实时保存到wav文件中,
但是不知道,怎么把这个数组 转化为声音强度,懂朋友请告知,能解决的话再加分。。
唯一就是实现不了,实时显示声音强度。有用过NAudio的朋友吗? 用这个实现也行。录音过程有一个回调事件,waveInStream_DataAvailable()
里面buffer, byte数组,就是把这些数据实时保存到wav文件中,
但是不知道,怎么把这个数组 转化为声音强度,懂朋友请告知,能解决的话再加分。。
/// method for retrieving the current volume from the system
/// </summary>
/// <returns>int value</returns>
public static int GetVolume()//这里就是获取音量大小
{
//method level variables
int currVolume;
int mixerControl; //create a new volume control
VolumeStructs.Mixer mixer = new VolumeStructs.Mixer();
//open the mixer
PCWin32.mixerOpen(out mixerControl, 0, 0, 0, 0); //set the type to volume control type
int type = VolumeConstants.MIXERCONTROL_CONTROLTYPE_VOLUME; //get the mixer control and get the current volume level
GetMixer(mixerControl, VolumeConstants.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, out mixer, out currVolume); //close the mixer control since we are now done with it
PCWin32.mixerClose(mixerControl); //return the current volume to the calling method
return currVolume;
}
/// <summary>
/// method for setting the volume to a specific level
/// </summary>
/// <param name="volumeLevel">volume level we wish to set volume to</param>
public static void SetVolume(int volumeLevel) //这里是设置音量大小
{
try
{
//method level variables
int currVolume;
int mixerControl; //create a new volume control
VolumeStructs.Mixer volumeControl = new VolumeStructs.Mixer(); //open the mixer control
PCWin32.mixerOpen(out mixerControl, 0, 0, 0, 0); //set the type to volume control type
int controlType = VolumeConstants.MIXERCONTROL_CONTROLTYPE_VOLUME; //get the current mixer control and get the current volume
GetMixer(mixerControl, VolumeConstants.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, controlType, out volumeControl, out currVolume); //now check the volume level. If the volume level
//is greater than the max then set the volume to
//the max level. If it's less than the minimum level
//then set it to the minimun level
if (volumeLevel > volumeControl.lMaximum)
{
volumeLevel = volumeControl.lMaximum;
}
else if (volumeLevel < volumeControl.lMinimum)
{
volumeLevel = volumeControl.lMinimum;
} //set the volume
SetMixer(mixerControl, volumeControl, volumeLevel); //now re-get the mixer control
GetMixer(mixerControl, VolumeConstants.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, controlType, out volumeControl, out currVolume); //make sure the volume level is equal to the current volume
if (volumeLevel != currVolume)
{
throw new Exception("Cannot Set Volume");
} //close the mixer control as we are finished with it
PCWin32.mixerClose(mixerControl);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}