Set the Volume of a specific ProcessID

Jul 26, 2014 at 1:10 PM
Hello,

thanks for this nice library, and the documentation.
But i have a problem. Can someone give me a piece of code how i can set the Volume of a ProcessID. It is certainly easy.
I tried somthing like this:


namespace TestEndpointVol_CSCORE
{
    class Program
    {
        static void Main(string[] args)
        {

            using (var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render))
            using (var sessionEnumerator = sessionManager.GetSessionEnumerator())
            {
                foreach (var session in sessionEnumerator)
                {
                    using (var session2 = session.QueryInterface<AudioSessionControl2>())
                    using (var EndpointVol = session.QueryInterface<AudioEndpointVolume>())
                    {

                        try
                        {
//???
                        }
                        catch { };
                    }
                }
            }
        }

        private static AudioSessionManager2 GetDefaultAudioSessionManager2(DataFlow dataFlow)
        {
           using (var enumerator = new MMDeviceEnumerator())
            {
                using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia))
                {
                    var sessionManager = AudioSessionManager2.FromMMDevice(device);
                    return sessionManager;
                }
            }
        }
Thanks for the help!

Best Regards,

Janek
Coordinator
Jul 26, 2014 at 1:19 PM
Edited Jul 26, 2014 at 1:26 PM
Replace
using (var EndpointVol = session.QueryInterface<AudioEndpointVolume>())
{

    try
    {
//???
    }
    catch { };
}
with
if(session2.ProcessID == yourProcessId)
{
    using (var simpleVolume = session.QueryInterface<SimpleAudioVolume>())
    {
        simpleVolume.MasterVolume = 0.5f; //set volume to 50%
    }
}
For more details see the Unit-Tests.
Marked as answer by filoe on 7/26/2014 at 7:33 AM
Jul 26, 2014 at 2:12 PM
Thanks filoe,

that works fine for me!

BR,

Janek