Fixed audio so it allows unplugged devices

Fixes #39. This update allows unplugged devices to be used. ALlows people to plug in headphones, and allows users like @gpo123 to use HDMI audio on a currently disbled display.
This commit is contained in:
Terry MacDonald 2021-10-17 11:45:06 +13:00
parent 81f2d08fe0
commit 953b72e6cd
3 changed files with 59 additions and 34 deletions

View File

@ -26,8 +26,8 @@ using System.Resources;
[assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")] [assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")]
// Version information // Version information
[assembly: AssemblyVersion("2.0.1.168")] [assembly: AssemblyVersion("2.0.1.171")]
[assembly: AssemblyFileVersion("2.0.1.168")] [assembly: AssemblyFileVersion("2.0.1.171")]
[assembly: NeutralResourcesLanguageAttribute( "en" )] [assembly: NeutralResourcesLanguageAttribute( "en" )]
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]

View File

@ -1326,7 +1326,9 @@ namespace DisplayMagician
if (worstError != ShortcutValidity.Error) if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Error; worstError = ShortcutValidity.Error;
} }
if (audioDevice.State == DeviceState.Unplugged) // As per Issue #39, this causes issues on HDMI audio devices and others that *could* work if the screen was enabled.
// Disabling this code as it is too much error checking for audio devices. The user can plug these in after the chagne and they will work.
/*if (audioDevice.State == DeviceState.Unplugged)
{ {
logger.Warn($"ShortcutRepository/RefreshValidity: Detected audio playback device {audioDevice.FullName} is the one we want, but it is unplugged!"); logger.Warn($"ShortcutRepository/RefreshValidity: Detected audio playback device {audioDevice.FullName} is the one we want, but it is unplugged!");
ShortcutError error = new ShortcutError(); ShortcutError error = new ShortcutError();
@ -1336,7 +1338,7 @@ namespace DisplayMagician
_shortcutErrors.Add(error); _shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error) if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning; worstError = ShortcutValidity.Warning;
} }*/
break; break;
} }
} }
@ -1399,7 +1401,9 @@ namespace DisplayMagician
if (worstError != ShortcutValidity.Error) if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Error; worstError = ShortcutValidity.Error;
} }
if (captureDevice.State == DeviceState.Unplugged) // As per Issue #39, this causes issues on HDMI audiodevices and others that *could* work if the screen was enabled.
// Disabling this code as it is too much error checking for capture devices. The user can plug these in after the chagne and they will work.
/*if (captureDevice.State == DeviceState.Unplugged)
{ {
logger.Warn($"ShortcutRepository/RefreshValidity: Detected capture device {captureDevice.FullName} is the one we want, but it is unplugged!"); logger.Warn($"ShortcutRepository/RefreshValidity: Detected capture device {captureDevice.FullName} is the one we want, but it is unplugged!");
ShortcutError error = new ShortcutError(); ShortcutError error = new ShortcutError();
@ -1409,7 +1413,7 @@ namespace DisplayMagician
_shortcutErrors.Add(error); _shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error) if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning; worstError = ShortcutValidity.Warning;
} }*/
break; break;
} }
} }

View File

@ -669,7 +669,7 @@ namespace DisplayMagician
} }
// Get the list of Audio Devices currently connected and active // Get the list of Audio Devices currently connected or unplugged (they can be plugged back in)
bool needToChangeAudioDevice = false; bool needToChangeAudioDevice = false;
CoreAudioDevice rollbackAudioDevice = null; CoreAudioDevice rollbackAudioDevice = null;
double rollbackAudioVolume = 50; double rollbackAudioVolume = 50;
@ -682,7 +682,8 @@ namespace DisplayMagician
if (_audioController != null) if (_audioController != null)
{ {
try { try {
activeAudioDevices = _audioController.GetPlaybackDevices(DeviceState.Active).ToList(); activeAudioDevices = _audioController.GetPlaybackDevices(DeviceState.Active | DeviceState.Unplugged).ToList();
bool foundAudioDevice = false;
if (activeAudioDevices.Count > 0) if (activeAudioDevices.Count > 0)
{ {
// Change Audio Device (if one specified) // Change Audio Device (if one specified)
@ -705,6 +706,7 @@ namespace DisplayMagician
if (needToChangeAudioDevice) if (needToChangeAudioDevice)
{ {
logger.Info($"ShortcutRepository/RunShortcut: Changing to the {shortcutToUse.AudioDevice} audio device."); logger.Info($"ShortcutRepository/RunShortcut: Changing to the {shortcutToUse.AudioDevice} audio device.");
foreach (CoreAudioDevice audioDevice in activeAudioDevices) foreach (CoreAudioDevice audioDevice in activeAudioDevices)
{ {
@ -712,29 +714,38 @@ namespace DisplayMagician
{ {
// use the Audio Device // use the Audio Device
audioDevice.SetAsDefault(); audioDevice.SetAsDefault();
foundAudioDevice = true;
break; break;
} }
} }
if (!foundAudioDevice)
{
logger.Error($"ShortcutRepository/RunShortcut: We wanted to use {shortcutToUse.AudioDevice} audio device but it wasn't plugged in or unplugged. Unable to use so skipping setting the audio device.");
}
} }
else else
{ {
logger.Info($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.AudioDevice} audio device so no need to change audio devices."); logger.Info($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.AudioDevice} audio device so no need to change audio devices.");
} }
if (shortcutToUse.SetAudioVolume) if (foundAudioDevice)
{ {
logger.Info($"ShortcutRepository/RunShortcut: Setting {shortcutToUse.AudioDevice} volume level to {shortcutToUse.AudioVolume}%."); if (shortcutToUse.SetAudioVolume)
Task myTask = new Task(() =>
{ {
_audioController.DefaultPlaybackDevice.SetVolumeAsync(Convert.ToDouble(shortcutToUse.AudioVolume)); logger.Info($"ShortcutRepository/RunShortcut: Setting {shortcutToUse.AudioDevice} volume level to {shortcutToUse.AudioVolume}%.");
}); Task myTask = new Task(() =>
myTask.Start(); {
myTask.Wait(2000); _audioController.DefaultPlaybackDevice.SetVolumeAsync(Convert.ToDouble(shortcutToUse.AudioVolume));
} });
else myTask.Start();
{ myTask.Wait(2000);
logger.Info($"ShortcutRepository/RunShortcut: We don't need to set the {shortcutToUse.AudioDevice} volume level."); }
} else
{
logger.Info($"ShortcutRepository/RunShortcut: We don't need to set the {shortcutToUse.AudioDevice} volume level.");
}
}
} }
else else
{ {
@ -754,8 +765,9 @@ namespace DisplayMagician
try try
{ {
// Get the list of Audio Devices currently connected // Get the list of Capture Devices currently connected or currently unplugged (they can be plugged back in)
activeCaptureDevices = _audioController.GetCaptureDevices(DeviceState.Active).ToList(); activeCaptureDevices = _audioController.GetCaptureDevices(DeviceState.Active | DeviceState.Unplugged).ToList();
bool foundCaptureDevice = false;
if (activeCaptureDevices.Count > 0) if (activeCaptureDevices.Count > 0)
{ {
@ -784,29 +796,38 @@ namespace DisplayMagician
{ {
// use the Audio Device // use the Audio Device
captureDevice.SetAsDefault(); captureDevice.SetAsDefault();
foundCaptureDevice = true;
break; break;
} }
} }
if (!foundCaptureDevice)
{
logger.Error($"ShortcutRepository/RunShortcut: We wanted to use {shortcutToUse.CaptureDevice} capture (microphone) device but it wasn't plugged in or unplugged. Unable to use so skipping setting the capture device.");
}
} }
else else
{ {
logger.Info($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.CaptureDevice} capture (microphone) device so no need to change capture devices."); logger.Info($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.CaptureDevice} capture (microphone) device so no need to change capture devices.");
} }
if (shortcutToUse.SetCaptureVolume) if (foundCaptureDevice)
{ {
logger.Info($"ShortcutRepository/RunShortcut: Setting {shortcutToUse.CaptureDevice} capture (microphone) level to {shortcutToUse.CaptureVolume}%."); if (shortcutToUse.SetCaptureVolume)
Task myTask = new Task(() =>
{ {
_audioController.DefaultCaptureDevice.SetVolumeAsync(Convert.ToDouble(shortcutToUse.CaptureVolume)); logger.Info($"ShortcutRepository/RunShortcut: Setting {shortcutToUse.CaptureDevice} capture (microphone) level to {shortcutToUse.CaptureVolume}%.");
}); Task myTask = new Task(() =>
myTask.Start(); {
myTask.Wait(2000); _audioController.DefaultCaptureDevice.SetVolumeAsync(Convert.ToDouble(shortcutToUse.CaptureVolume));
} });
else myTask.Start();
{ myTask.Wait(2000);
logger.Info($"ShortcutRepository/RunShortcut: We don't need to set the {shortcutToUse.CaptureDevice} capture (microphone) volume level."); }
} else
{
logger.Info($"ShortcutRepository/RunShortcut: We don't need to set the {shortcutToUse.CaptureDevice} capture (microphone) volume level.");
}
}
} }
else else