Skip to content

Instantly share code, notes, and snippets.

@NickCraver
Created March 27, 2015 03:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickCraver/2f004349401822b74db5 to your computer and use it in GitHub Desktop.
Save NickCraver/2f004349401822b74db5 to your computer and use it in GitHub Desktop.
A simple HTTPListener to enable muting remotely
using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
class Program
{
static readonly HttpListener httpListener = new HttpListener();
static void Main(string[] args)
{
httpListener.Prefixes.Add("http://*:9000/");
httpListener.Start();
Console.WriteLine("Listening, press any key to exit.");
httpListener.BeginGetContext(GetContextCallback, null);
Console.ReadKey();
httpListener.Stop();
}
public static void GetContextCallback(IAsyncResult result)
{
var context = httpListener.EndGetContext(result);
var path = context.Request.Url.LocalPath;
if (path.StartsWith("/mute"))
Marshal.ThrowExceptionForHR(GetMaster().SetMute(true, Guid.Empty));
else if (path.StartsWith("/unmute"))
Marshal.ThrowExceptionForHR(GetMaster().SetMute(false, Guid.Empty));
var buffer = Encoding.UTF8.GetBytes(path);
context.Response.ContentLength64 = buffer.Length;
using (var outputStream = context.Response.OutputStream)
outputStream.Write(buffer, 0, buffer.Length);
httpListener.BeginGetContext(GetContextCallback, null);
}
private static IAudioEndpointVolume GetMaster()
{
var deviceEnum = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
IMMDevice defaultDevice;
Marshal.ThrowExceptionForHR(deviceEnum.GetDefaultAudioEndpoint(0, 1, out defaultDevice));
IAudioEndpointVolume result;
var aevID = typeof(IAudioEndpointVolume).GUID;
Marshal.ThrowExceptionForHR(defaultDevice.Activate(ref aevID, 23, 0, out result));
return result;
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
class MMDeviceEnumerator { }
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IMMDeviceEnumerator
{
int f(); // You have to have this, don't ask questions.
int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IMMDevice
{
int Activate(ref Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume pointer);
}
[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IAudioEndpointVolume
{
int f(); int g(); int h(); int i(); int j(); int k(); int l(); int m(); int n();
int SetMasterVolumeLevelScalar(float fLevel, Guid pguidEventContext);
int GetMasterVolumeLevelScalar(out float pfLevel);
int SetMute([MarshalAs(UnmanagedType.Bool)] Boolean bMute, Guid pguidEventContext);
int GetMute(out bool pbMute);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment