Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AudioSwitch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<Compile Include="CoreAudioApi\AudioVolumeNotificationData.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="CoreAudioApi\DeviceNotFoundException.cs" />
<Compile Include="CoreAudioApi\EDeviceState.cs" />
<Compile Include="CoreAudioApi\EDataFlow.cs">
<SubType>Code</SubType>
Expand Down
6 changes: 5 additions & 1 deletion Classes/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ private static void Main(string[] args)

try
{
#if !DEBUG
//log and handle error only if not debugging
AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
#endif
settings = Settings.Load();

if (args.Length > 0)
Expand Down Expand Up @@ -225,7 +228,7 @@ private static void Main(string[] args)
}
}
}

#if !DEBUG
static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
Expand All @@ -250,6 +253,7 @@ static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEve
Application.Exit();
}
}
#endif
}
}

9 changes: 8 additions & 1 deletion Controls/VolumeBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,14 @@ private void VolNotify(AudioVolumeNotificationData data)

internal void RegisterDevice(EDataFlow RenderType)
{
Device = EndPoints.DeviceEnumerator.GetDefaultAudioEndpoint(RenderType, ERole.eMultimedia);
try
{
Device = EndPoints.DeviceEnumerator.GetDefaultAudioEndpoint(RenderType, ERole.eMultimedia);
}
catch (DeviceNotFoundException)
{//According to previous code state further code won't be called if no devices
return;
}
Value = Device.AudioEndpointVolume.MasterVolumeLevelScalar;
Mute = Device.AudioEndpointVolume.Mute;
Stereo = Device.AudioMeterInformation.Channels.GetCount() > 1;
Expand Down
17 changes: 17 additions & 0 deletions CoreAudioApi/DeviceNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Runtime.InteropServices;

namespace AudioSwitch.CoreAudioApi
{
//Inheriting COMException for backward compatibility (the same for HResult in ctor)
public class DeviceNotFoundException : COMException
{
// ReSharper disable once InconsistentNaming
internal const int E_NOTFOUND = -2147023728;

internal DeviceNotFoundException(string message, Exception inner) : base(message, inner)
{
HResult = E_NOTFOUND;//https://msdn.microsoft.com/en-us/library/windows/desktop/dd371401(v=vs.85).aspx
}
}
}
22 changes: 19 additions & 3 deletions CoreAudioApi/MMDeviceEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,27 @@ public MMDeviceCollection EnumerateAudioEndPoints(EDataFlow dataFlow, EDeviceSta
return new MMDeviceCollection(result);
}

/// <summary>
///
/// </summary>
/// <param name="dataFlow"></param>
/// <param name="role"></param>
/// <returns></returns>
/// <exception cref="COMException"></exception>
/// <exception cref="DeviceNotFoundException">If there is no default device</exception>
public MMDevice GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role)
{
IMMDevice _Device;
Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(dataFlow, role, out _Device));
return new MMDevice(_Device);
try
{
IMMDevice _Device;
Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(dataFlow, role, out _Device));
return new MMDevice(_Device);

}
catch (COMException comException) when (comException.ErrorCode == DeviceNotFoundException.E_NOTFOUND)
{//if COMException will be used the same way in other methods consider create Exception Mapper instead of manual try/catch
throw new DeviceNotFoundException("No devices found during default device detection",comException);
}
}

public MMDevice GetDevice(string deviceId)
Expand Down
23 changes: 12 additions & 11 deletions Forms/FormSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ public FormSwitcher()
SetTrayIcons();

VolBar.VolumeMuteChanged += IconChanged;
if (listDevices.Items.Count > 0)
VolBar.RegisterDevice(RenderType);
VolBar.RegisterDevice(RenderType);

EndPoints.NotifyClient.DefaultChanged += DefaultChanged;
EndPoints.NotifyClient.DeviceAdded += DeviceAdded;
Expand Down Expand Up @@ -402,8 +401,7 @@ private void notifyIcon1_MouseUp(object sender, MouseEventArgs e)
return;
}

if (listDevices.Items.Count > 0)
VolBar.RegisterDevice(RenderType);
VolBar.RegisterDevice(RenderType);

if (e.Button == MouseButtons.Left)
{
Expand Down Expand Up @@ -503,13 +501,16 @@ private void SetDeviceIcon(int index, bool isSelected)

private void SetTrayIcons()
{
var devSettings = Program.settings.Device.Find(x =>
{
var dev = listDevices.SelectedItems[0];
return x.DeviceID == dev.Tag.ToString();
});
if (listDevices.SelectedItems.Count == 0 ||
devSettings == null ||
Settings.CDevice devSettings=null;

if (listDevices.SelectedItems.Count > 0)
devSettings = Program.settings.Device.Find(x =>
{
var dev = listDevices.SelectedItems[0];
return x.DeviceID == dev.Tag.ToString();
});

if (devSettings == null ||
(devSettings.Hue == 0 &&
devSettings.Saturation == 0 &&
devSettings.Brightness == 0))
Expand Down