The Enterprise Eightfold Path

QuickPost: How to automatically mute the volume on XenDesktop sessions (or set it to a specific percentage)

The Enterprise Eightfold Path

QuickPost: How to automatically mute the volume on XenDesktop sessions (or set it to a specific percentage)

By James Rankin   |     Tuesday 3 November 2015

Tags:

Recently I was deploying a few XenDesktop instances into thin clients on open-access (library) areas. One of the requirements was to automatically mute the volume on these Windows 10 machines so people couldn’t disturb their neighbours.

Naturally the first suggestion was to simply disable or stop the Windows Audio Service, but we wanted users to be able to use headphones and the like, not prevent them from having sound altogether. The idea was that the default setting would always be muted – users could then turn the mute setting off manually when they plugged in their headphones or headsets.

We could have set the volume to muted in the default profile on the images also, but given that we were using local profiles if the user changed the mute settings this would then have persisted, so it was also not an option.

As to how to achieve this programatically, well of course a bit of PowerShell would do. This is what I used (not my own code, this was unearthed by the power of Google-fu) – lines may wrap:

Add-Type -TypeDefinition @’
using System.Runtime.InteropServices;
 
[Guid(“5CDF2C82-841E-4546-9722-0CF74078229A”), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume {
  int f(); int g(); int h(); int i();
  int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
  int j();
  int GetMasterVolumeLevelScalar(out float pfLevel);
  int k(); int l(); int m(); int n();
  int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
  int GetMute(out bool pbMute);
}
[Guid(“D666063F-1587-4E43-81F1-B948E807363F”), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice {
  int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
}
[Guid(“A95664D2-9614-4F35-A746-DE8DB63617E6”), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator {
  int f(); // Unused
  int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid(“BCDE0395-E52F-467C-8E3D-C4579291692E”)] class MMDeviceEnumeratorComObject { }
 
public class Audio {
  static IAudioEndpointVolume Vol() {
    var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
    IMMDevice dev = null;
    Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
    IAudioEndpointVolume epv = null;
    var epvid = typeof(IAudioEndpointVolume).GUID;
    Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
    return epv;
  }
  public static float Volume {
    get {float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v;}
    set {Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty));}
  }
  public static bool Mute {
    get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
    set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
  }
}
‘@
 
[Audio]::Mute = $true

I put this together in an AppSense Desktop Created Action, but as usual, you can deploy this any way you should so desire.

When the users logged in to their XenDesktop sessions next, the volume was automatically muted and their neighbours could continue to work in peace 🙂

On the flip side, if you wanted to set the volume to unmuted and 100% (or any specific percentage, maybe for lecture theatre machines and the like), then replace the last line above with these

[Audio]::Volume = 1.0

[Audio]::Mute = $false

Obviously if you wanted 50% instead of 100%, change the 1.0 to 0.5 – you get the idea.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Join our mailing list

Sign up for our newsletter today and we'll send you exclusive content including free guides and articles. We promise not to send you spam and we don't share your details with anybody else.

Contact us

Howell Technology Group
One Trinity Green
Eldon Street
South Shields
NE33 1SA

T. 0191 4813446

Email us

Cookies policy

The HTG website uses cookies to store information on your computer. By continuing to browse this website you are agreeing to our use of cookies. Learn more

Accept

Thank you - you've accepted our cookies policy.