Possible fix for volume-control

This commit is contained in:
Johnny @PC 2015-12-22 20:44:42 +01:00
parent 9d84808fa4
commit 82d1af1a4e

View File

@ -1,20 +1,17 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace SpotifyAPI.Local namespace SpotifyAPI.Local
{ {
internal class VolumeMixerControl internal static class VolumeMixerControl
{ {
private const String SpotifyProcessName = "spotify"; private const String SpotifyProcessName = "spotify";
internal static float GetSpotifyVolume() internal static float GetSpotifyVolume()
{ {
Process[] p = Process.GetProcessesByName(SpotifyProcessName); int pid = GetSpotifyPid();
if (p.Length == 0)
throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid); ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
@ -30,11 +27,7 @@ namespace SpotifyAPI.Local
internal static bool IsSpotifyMuted() internal static bool IsSpotifyMuted()
{ {
Process[] p = Process.GetProcessesByName(SpotifyProcessName); int pid = GetSpotifyPid();
if (p.Length == 0)
throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid); ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
@ -50,11 +43,7 @@ namespace SpotifyAPI.Local
internal static void SetSpotifyVolume(float level) internal static void SetSpotifyVolume(float level)
{ {
Process[] p = Process.GetProcessesByName(SpotifyProcessName); int pid = GetSpotifyPid();
if (p.Length == 0)
throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid); ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
@ -69,11 +58,7 @@ namespace SpotifyAPI.Local
internal static void MuteSpotify(bool mute) internal static void MuteSpotify(bool mute)
{ {
Process[] p = Process.GetProcessesByName(SpotifyProcessName); int pid = GetSpotifyPid();
if (p.Length == 0)
throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid); ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
@ -86,6 +71,20 @@ namespace SpotifyAPI.Local
Marshal.ReleaseComObject(volume); Marshal.ReleaseComObject(volume);
} }
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) private static ISimpleAudioVolume GetVolumeObject(int pid)
{ {
// get the speakers (1st render + multimedia) device // get the speakers (1st render + multimedia) device