Merge pull request #90 from mfro/master

Fixed bug with volume control when spotify is in system tray
This commit is contained in:
Jonas Dellinger 2016-07-16 18:32:11 +02:00 committed by GitHub
commit 74c7761d6d

View File

@ -11,9 +11,8 @@ namespace SpotifyAPI.Local
internal static float GetSpotifyVolume() internal static float GetSpotifyVolume()
{ {
int pid = GetSpotifyPid(); ISimpleAudioVolume volume = GetSpotifyVolumeObject();
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
{ {
throw new COMException("Volume object creation failed"); throw new COMException("Volume object creation failed");
@ -27,9 +26,8 @@ namespace SpotifyAPI.Local
internal static bool IsSpotifyMuted() internal static bool IsSpotifyMuted()
{ {
int pid = GetSpotifyPid(); ISimpleAudioVolume volume = GetSpotifyVolumeObject();
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
{ {
throw new COMException("Volume object creation failed"); throw new COMException("Volume object creation failed");
@ -43,9 +41,8 @@ namespace SpotifyAPI.Local
internal static void SetSpotifyVolume(float level) internal static void SetSpotifyVolume(float level)
{ {
int pid = GetSpotifyPid(); ISimpleAudioVolume volume = GetSpotifyVolumeObject();
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
{ {
throw new COMException("Volume object creation failed"); throw new COMException("Volume object creation failed");
@ -58,9 +55,8 @@ namespace SpotifyAPI.Local
internal static void MuteSpotify(bool mute) internal static void MuteSpotify(bool mute)
{ {
int pid = GetSpotifyPid(); ISimpleAudioVolume volume = GetSpotifyVolumeObject();
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
{ {
throw new COMException("Volume object creation failed"); throw new COMException("Volume object creation failed");
@ -71,18 +67,11 @@ namespace SpotifyAPI.Local
Marshal.ReleaseComObject(volume); Marshal.ReleaseComObject(volume);
} }
private static int GetSpotifyPid() private static ISimpleAudioVolume GetSpotifyVolumeObject() {
{ return (from p in Process.GetProcessesByName(SpotifyProcessName)
Process[] processes = Process.GetProcessesByName(SpotifyProcessName); let vol = GetVolumeObject(p.Id)
if (processes.Length == 0) where vol != null
throw new Exception("Spotify process is not running or was not found!"); select vol).FirstOrDefault();
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)