I'm using the Windows Core Audio API, with a wrapper from Controlling Application's Volume: By Process-ID
In this test method here, which sets the volume of a application (by ProcessID):
[TestMethod]
public void CanChangeVolume()
{
VolumeMixer.SetApplicationVolume(19348, 50f);
}
If i manually enter 19348
into the code, the function works as it should.
But if i pass a dynamic value, like so...
[TestMethod]
public void CanChangeVolume()
{
foreach (AudioSession session in AudioUtilities.GetAllSessions())
{
if (session.Process != null)
{
VolumeMixer.SetApplicationVolume(session.ProcessId, 50f);
}
}
}
... i get the following error by the Core Audio API:
System.InvalidCastException: Unable to cast object of type 'MMDeviceEnumerator' to type 'RemoteVolume.Server.MMDeviceEnumerator'.
Note: Printing out session.ProcessId
is also working fine...
For Example: session.ProcessId = 8508
, VolumeMixer.SetApplicationVolume(8508, 50f)
is working fine