C# 获取麦克风音量并设置音量,获取扬声器音量
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using NAudio.CoreAudioApi;
using System.Collections.Generic;
using System.Linq;
namespace WpfApp2
{
public class TestV
{
//获取系统麦克风音量
public int GetCurrentSpeakerVolume()
{
int volume = 0;
var enumerator = new MMDeviceEnumerator();
//获取音频输出设备
IEnumerable<MMDevice> speakDevices = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active).ToArray();
if (speakDevices.Count() > 0)
{
MMDevice mMDevice = speakDevices.ToList()[0];
volume = Convert.ToInt16(mMDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
}
return volume;
}
// 获取当前系统扬声器音量
public int GetCurrentSpeakerVolume1()
{
int volume = 0;
var enumerator = new MMDeviceEnumerator();
//获取音频输出设备
IEnumerable<MMDevice> speakDevices = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active).ToArray();
if (speakDevices.Count() > 0)
{
MMDevice mMDevice = speakDevices.ToList()[0];
volume = Convert.ToInt16(mMDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
}
return volume;
}
//设置当前系统麦克风音量 volume 取值 0-100
private void GetCurrentSpeakerVolume(int volume)
{
var enumerator = new MMDeviceEnumerator();
IEnumerable<MMDevice> speakDevices = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active).ToArray();
if (speakDevices.Count() > 0)
{
MMDevice mMDevice = speakDevices.ToList()[0];
mMDevice.AudioEndpointVolume.MasterVolumeLevelScalar = volume / 100.0f;
}
}
}
}