How to adjust microphone gain from C# (needs to work on XP & W7)

前端 未结 2 624
长发绾君心
长发绾君心 2020-12-17 01:51

First, note that I know there are a few questions like this already posted; however they don\'t seem to address the problem adequately. I have a C# application, with all th

2条回答
  •  眼角桃花
    2020-12-17 02:02

    I tried doing exactly this a while ago when I was writing .NET Voice Recorder using NAudio, and found it extremely hard. You probably have to end up writing two lots of code, one for XP and one for Vista/Win 7. I am using NAudio for the mixer interop.

    This is what I ended up with (still doesn't work everywhere)

        private void TryGetVolumeControl()
        {
            int waveInDeviceNumber = waveIn.DeviceNumber;
            if (Environment.OSVersion.Version.Major >= 6) // Vista and over
            {
                var mixerLine = new MixerLine((IntPtr)waveInDeviceNumber, 0, MixerFlags.WaveIn);
                foreach (var control in mixerLine.Controls)
                {
                    if (control.ControlType == MixerControlType.Volume)
                    {
                        volumeControl = control as UnsignedMixerControl;
                        MicrophoneLevel = desiredVolume;
                        break;
                    }
                }
            }
            else
            {
                var mixer = new Mixer(waveInDeviceNumber);
                foreach (var destination in mixer.Destinations)
                {
                    if (destination.ComponentType == MixerLineComponentType.DestinationWaveIn)
                    {
                        foreach (var source in destination.Sources)
                        {
                            if (source.ComponentType == MixerLineComponentType.SourceMicrophone)
                            {
                                foreach (var control in source.Controls)
                                {
                                    if (control.ControlType == MixerControlType.Volume)
                                    {
                                        volumeControl = control as UnsignedMixerControl;
                                        MicrophoneLevel = desiredVolume;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
    
        }
    

提交回复
热议问题