Controling Volume Mixer

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

I want to control other application volume(firefox).

i can do it with Volume Mixer

What is the libraries of the Volume Mixer?

回答1:

Here is a sample C# Console Application that does it. It's based on the Windows Core Audio Library. It works only on Windows 7 and higher.

using System; using System.Runtime.InteropServices; using System.Collections.Generic;  namespace SetAppVolumne {     class Program     {         static void Main(string[] args)         {             const string app = "Mozilla Firefox";              foreach (string name in EnumerateApplications())             {                 Console.WriteLine("name:" + name);                 if (name == app)                 {                     // display mute state & volume level (% of master)                     Console.WriteLine("Mute:" + GetApplicationMute(app));                     Console.WriteLine("Volume:" + GetApplicationVolume(app));                      // mute the application                     SetApplicationMute(app, true);                      // set the volume to half of master volume (50%)                     SetApplicationVolume(app, 50);                 }             }         }          public static float? GetApplicationVolume(string name)         {             ISimpleAudioVolume volume = GetVolumeObject(name);             if (volume == null)                 return null;              float level;             volume.GetMasterVolume(out level);             return level * 100;         }          public static bool? GetApplicationMute(string name)         {             ISimpleAudioVolume volume = GetVolumeObject(name);             if (volume == null)                 return null;              bool mute;             volume.GetMute(out mute);             return mute;         }          public static void SetApplicationVolume(string name, float level)         {             ISimpleAudioVolume volume = GetVolumeObject(name);             if (volume == null)                 return;              Guid guid = Guid.Empty;             volume.SetMasterVolume(level / 100, ref guid);         }          public static void SetApplicationMute(string name, bool mute)         {             ISimpleAudioVolume volume = GetVolumeObject(name);             if (volume == null)                 return;              Guid guid = Guid.Empty;             volume.SetMute(mute, ref guid);         }          public static IEnumerable EnumerateApplications()         {             // get the speakers (1st render + multimedia) device             IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());             IMMDevice speakers;             deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, 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 

Note: I have not defined the interfaces completely, only what was needed for the code to work.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!