Spotify.NET/SpotifyAPI/Local/VolumeMixerControl.cs

253 lines
8.1 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
2015-12-22 19:44:42 +00:00
using System.Linq;
using System.Runtime.InteropServices;
namespace SpotifyAPI.Local
{
2015-12-22 19:44:42 +00:00
internal static class VolumeMixerControl
{
2015-10-28 16:05:09 +00:00
private const String SpotifyProcessName = "spotify";
internal static float GetSpotifyVolume()
{
2015-12-22 19:44:42 +00:00
int pid = GetSpotifyPid();
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
{
throw new COMException("Volume object creation failed");
}
float level;
volume.GetMasterVolume(out level);
Marshal.ReleaseComObject(volume);
return level * 100;
}
internal static bool IsSpotifyMuted()
{
2015-12-22 19:44:42 +00:00
int pid = GetSpotifyPid();
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
{
throw new COMException("Volume object creation failed");
}
bool mute;
volume.GetMute(out mute);
Marshal.ReleaseComObject(volume);
return mute;
}
internal static void SetSpotifyVolume(float level)
{
2015-12-22 19:44:42 +00:00
int pid = GetSpotifyPid();
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
{
throw new COMException("Volume object creation failed");
}
Guid guid = Guid.Empty;
volume.SetMasterVolume(level / 100, ref guid);
Marshal.ReleaseComObject(volume);
}
internal static void MuteSpotify(bool mute)
{
2015-12-22 19:44:42 +00:00
int pid = GetSpotifyPid();
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
{
throw new COMException("Volume object creation failed");
}
Guid guid = Guid.Empty;
volume.SetMute(mute, ref guid);
Marshal.ReleaseComObject(volume);
}
2015-12-22 19:44:42 +00:00
private static int GetSpotifyPid()
{
Process[] processes = Process.GetProcessesByName(SpotifyProcessName);
if (processes.Length == 0)
throw new Exception("Spotify process is not running or was not found!");
Process mainProc = processes.FirstOrDefault(o => o.MainWindowHandle != IntPtr.Zero);
if(mainProc == null)
throw new Exception("Spotify main-process is not running or was not found!");
return mainProc.Id;
}
private static ISimpleAudioVolume GetVolumeObject(int pid)
{
// get the speakers (1st render + multimedia) device
2015-10-28 16:05:09 +00:00
IMmDeviceEnumerator deviceEnumerator = (IMmDeviceEnumerator)(new MMDeviceEnumerator());
IMmDevice speakers;
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out speakers);
// activate the session manager. we need the enumerator
2015-10-28 16:05:09 +00:00
Guid iidIAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
object o;
2015-10-28 16:05:09 +00:00
speakers.Activate(ref iidIAudioSessionManager2, 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);
// search for an audio session with the required name
// NOTE: we could also use the process id instead of the app name (with IAudioSessionControl2)
ISimpleAudioVolume volumeControl = null;
for (int i = 0; i < count; i++)
{
IAudioSessionControl2 ctl;
sessionEnumerator.GetSession(i, out ctl);
int cpid;
ctl.GetProcessId(out cpid);
if (cpid == pid)
{
2015-10-28 16:05:09 +00:00
volumeControl = (ISimpleAudioVolume) ctl;
break;
}
Marshal.ReleaseComObject(ctl);
}
Marshal.ReleaseComObject(sessionEnumerator);
Marshal.ReleaseComObject(mgr);
Marshal.ReleaseComObject(speakers);
Marshal.ReleaseComObject(deviceEnumerator);
return volumeControl;
}
[ComImport]
[Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
private class MMDeviceEnumerator
{
}
private enum EDataFlow
{
2015-10-28 16:05:09 +00:00
ERender,
ECapture,
EAll,
EDataFlowEnumCount
}
private enum ERole
{
2015-10-28 16:05:09 +00:00
EConsole,
EMultimedia,
ECommunications,
ERoleEnumCount
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
2015-10-28 16:05:09 +00:00
private interface IMmDeviceEnumerator
{
int NotImpl1();
[PreserveSig]
2015-10-28 16:05:09 +00:00
int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMmDevice ppDevice);
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
2015-10-28 16:05:09 +00:00
private interface IMmDevice
{
[PreserveSig]
int Activate(ref Guid iid, int dwClsCtx, IntPtr pActivationParams, [MarshalAs(UnmanagedType.IUnknown)] out object ppInterface);
}
[Guid("77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IAudioSessionManager2
{
int NotImpl1();
int NotImpl2();
[PreserveSig]
2015-10-28 16:05:09 +00:00
int GetSessionEnumerator(out IAudioSessionEnumerator sessionEnum);
}
[Guid("E2F5BB11-0570-40CA-ACDD-3AA01277DEE8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IAudioSessionEnumerator
{
[PreserveSig]
2015-10-28 16:05:09 +00:00
int GetCount(out int sessionCount);
[PreserveSig]
2015-10-28 16:05:09 +00:00
int GetSession(int sessionCount, out IAudioSessionControl2 session);
}
[Guid("87CE5498-68D6-44E5-9215-6DA47EF883D8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface ISimpleAudioVolume
{
[PreserveSig]
2015-10-28 16:05:09 +00:00
int SetMasterVolume(float fLevel, ref Guid eventContext);
[PreserveSig]
int GetMasterVolume(out float pfLevel);
[PreserveSig]
2015-10-28 16:05:09 +00:00
int SetMute(bool bMute, ref Guid eventContext);
[PreserveSig]
int GetMute(out bool pbMute);
}
[Guid("bfb7ff88-7239-4fc9-8fa2-07c950be9c6d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IAudioSessionControl2
{
[PreserveSig]
int NotImpl0();
[PreserveSig]
int GetDisplayName([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
[PreserveSig]
2015-10-28 16:05:09 +00:00
int SetDisplayName([MarshalAs(UnmanagedType.LPWStr)]string value, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[PreserveSig]
int GetIconPath([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
[PreserveSig]
2015-10-28 16:05:09 +00:00
int SetIconPath([MarshalAs(UnmanagedType.LPWStr)] string value, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[PreserveSig]
int GetGroupingParam(out Guid pRetVal);
[PreserveSig]
2015-10-28 16:05:09 +00:00
int SetGroupingParam([MarshalAs(UnmanagedType.LPStruct)] Guid Override, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[PreserveSig]
int NotImpl1();
[PreserveSig]
int NotImpl2();
[PreserveSig]
int GetSessionIdentifier([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
[PreserveSig]
int GetSessionInstanceIdentifier([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
[PreserveSig]
int GetProcessId(out int pRetVal);
[PreserveSig]
int IsSystemSoundsSession();
[PreserveSig]
int SetDuckingPreference(bool optOut);
}
}
}