3

I want to change sound level of local computer's microphone. I research so much I found this links

--> It is give codeproject link but it is so mix project and I want to change only microphone sound level

Set microphone volume in C#

--> and this gives a project again and it is so compilcated and ı could not build project.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/177779cd-28b9-4eae-8bc4-61263e8a876b/getting-and-setting-microphone-device-volume-using-c-code?forum=netfxbcl

and ı try this.

it is work only for exe's voice level like lync,skype. And It need processes name. Microphone exe could be changed frequently. So I could not give stable name. I want to try win32 change So how can I change microphone sound level ? ı want to increase and decrease sound level

public static void SetMicrophoneVolume(string name, float level)

full code here

   private static List<ISimpleAudioVolume> getAppVol(string name, EDataFlow eDataFlow)
{
  List<ISimpleAudioVolume> simpleAudioVolList = new System.Collections.Generic.List<ISimpleAudioVolume>();
  // get the speakers (1st render + multimedia) device
  IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
  IMMDevice speakers;
  deviceEnumerator.GetDefaultAudioEndpoint(eDataFlow, ERole.eMultimedia, out speakers);

  // activate the session manager. we need the enumerator
  Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
  object o;
  speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o);
  IAudioSessionManager2 mgr = (IAudioSessionManager2)o;

  // enumerate sessions for on this device
  IAudioSessionEnumerator sessionEnumerator;
  mgr.GetSessionEnumerator(out sessionEnumerator);
  int count;
  sessionEnumerator.GetCount(out count);

  for(int i = 0; i < count; i++)
  {
    IAudioSessionControl ctl;
    sessionEnumerator.GetSession(i, out ctl);
    IAudioSessionControl2 sessionCtl = ctl as IAudioSessionControl2;
    uint processId;
    string displayName;
    string iconPath;
    string sessionIdentifier;
    string sessionInstanceIdentifier;
    sessionCtl.GetProcessId(out processId);
    sessionCtl.GetDisplayName(out displayName);
    sessionCtl.GetIconPath(out iconPath);
    sessionCtl.GetSessionIdentifier(out sessionIdentifier);
    sessionCtl.GetSessionInstanceIdentifier(out sessionInstanceIdentifier);
    Debug.WriteLine("process id:" + processId + " name:" + displayName + " icon:" + iconPath + " sessionIdentifier:" + sessionIdentifier + " sessionInstanceIdentifier:" + sessionInstanceIdentifier);
    //Process process = Process.GetProcessesByName(name).FirstOrDefault<Process>(delegate(Process item) { return processId == item.Id; });
    if(sessionIdentifier.Contains(name))
    {
      simpleAudioVolList.Add((ISimpleAudioVolume)sessionCtl);
    }
  }
  Marshal.ReleaseComObject(sessionEnumerator);
  Marshal.ReleaseComObject(mgr);
  Marshal.ReleaseComObject(speakers);
  Marshal.ReleaseComObject(deviceEnumerator);
  return simpleAudioVolList;
}

private static void releasesimpleAudioVolList(List<ISimpleAudioVolume> volumeControlList)
{
  foreach(ISimpleAudioVolume item in volumeControlList)
  {
    Marshal.ReleaseComObject(item);
  }
}

public static void SetMicrophoneVolume(string name, float level)
{
  List<ISimpleAudioVolume> volumeList = getAppVol(name, EDataFlow.eCapture);

  Guid guid = Guid.Empty;
  foreach(ISimpleAudioVolume item in volumeList)
  {
    try
    {
      item.SetMasterVolume(level / 100, ref guid);
    }
    catch(Exception ex)
    {
      Debug.WriteLine(ex.ToString());
    }
  }
  releasesimpleAudioVolList(volumeList);
  return;
}
Community
  • 1
  • 1
adk
  • 31
  • 3
  • I think it depends on your version of windows as the volume of playback devices has the same issue. Maybe you want to specify it? http://stackoverflow.com/questions/15250379/change-master-audio-volume-from-xp-to-windows-8-in-c-sharp – online Thomas Oct 15 '15 at 08:42
  • I do not want to add outsource library which is not included c# .and ı try this code. I want to change local pc 's microphone not speaker or other exe. – adk Oct 15 '15 at 08:45
  • I just made a reference because your problem needs a different solution per windows version and you might want to specify which versions of Windows you want to support. That's all. – online Thomas Oct 15 '15 at 08:50
  • thank you but İf ı could change win32, it can be all of windows version I think – adk Oct 15 '15 at 08:52

0 Answers0