如何显示AudioMeterInformation?
我需要显示默认捕获设备和输出设备的计数,类似于在Windows 7中打开Windows“播放和记录设备”对话框时的情况。
有一个可用的开源API,它包装在名为naudio的Windows核心音频上。提供的示例使用WaveIn设备收集声音样本,然后产生计量信息
WaveIn = new WaveIn { DeviceNumber = 0 };
WaveIn.DataAvailable += OnDataAvailable;
WaveIn.RecordingStopped += OnRecordingStopped;
WaveIn.WaveFormat = new WaveFormat(44100, 1);
public virtual void OnDataAvailable(object sender, WaveInEventArgs e)
{
byte[] buffer = e.Buffer;
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((buffer[index + 1] << 8) | buffer[index + 0]);
float sample32 = sample / 32768f;
SampleAggregator.Add(sample32);
}
}
public class SampleAggregator
{
// volume
private int Count { get; set; }
public float MaxValue { get; set; }
public float MinValue { get; set; }
public int NotificationCount { get; set; }
public event EventHandler<MaxSampleEventArgs> MaximumCalculated;
public event EventHandler Restart = delegate { };
public void RaiseRestart()
{
Restart(this, EventArgs.Empty);
}
private void Reset()
{
Count = 0;
MaxValue = MinValue = 0;
}
public void Add(float value)
{
MaxValue = Math.Max(MaxValue, value);
MinValue = Math.Min(MinValue, value);
Count++;
if (Count >= NotificationCount && NotificationCount > 0)
{
if (MaximumCalculated != null)
{
MaximumCalculated(this, new MaxSampleEventArgs(MinValue, MaxValue));
}
Reset();
}
}
}
在没有AudioMeterInformation的Windows XP中进行采样很有意义。
有人可以帮助显示AudioMeterInformation,而不必从设备中取样。
最佳答案
尝试CoreAudioApi(CodeProject article)。
MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
MMDevice defaultDevice =
devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
// Call this line as often as you need.
int level = defaultDevice.AudioMeterInformation.MasterPeakValue;
关于c# - 如何显示AudioMeterInformation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11096625/