add some error handling for audio sessions

This commit is contained in:
dlprows 2024-10-02 18:06:27 -06:00
parent d1a5e37067
commit f5ab700817
2 changed files with 61 additions and 1 deletions

View File

@ -53,6 +53,11 @@ public class AudioHelper
{ {
deviceCollection.Item(d, out var device); deviceCollection.Item(d, out var device);
if(device == null)
{
continue;
}
Guid iid = typeof(IAudioSessionManager2).GUID; Guid iid = typeof(IAudioSessionManager2).GUID;
device.Activate(ref iid, CLSCTX.ALL, IntPtr.Zero, out var m); device.Activate(ref iid, CLSCTX.ALL, IntPtr.Zero, out var m);
var manager = (IAudioSessionManager2)m; var manager = (IAudioSessionManager2)m;
@ -61,6 +66,11 @@ public class AudioHelper
manager.GetSessionEnumerator(out var sessionEnumerator); manager.GetSessionEnumerator(out var sessionEnumerator);
if(sessionEnumerator == null)
{
continue;
}
var currentIndex = int.MaxValue; var currentIndex = int.MaxValue;
sessionEnumerator.GetCount(out var count); sessionEnumerator.GetCount(out var count);
@ -68,6 +78,11 @@ public class AudioHelper
{ {
sessionEnumerator.GetSession(i, out var session); sessionEnumerator.GetSession(i, out var session);
if(session == null)
{
continue;
}
session.GetProcessId(out var sessionProcessId); session.GetProcessId(out var sessionProcessId);
var audioProcess = GetProcessById(sessionProcessId); var audioProcess = GetProcessById(sessionProcessId);
@ -241,6 +256,11 @@ public class AudioHelper
{ {
deviceCollection.Item(d, out var device); deviceCollection.Item(d, out var device);
if(device == null)
{
continue;
}
Guid iid = typeof(IAudioSessionManager2).GUID; Guid iid = typeof(IAudioSessionManager2).GUID;
device.Activate(ref iid, CLSCTX.ALL, IntPtr.Zero, out var m); device.Activate(ref iid, CLSCTX.ALL, IntPtr.Zero, out var m);
var manager = (IAudioSessionManager2)m; var manager = (IAudioSessionManager2)m;
@ -248,11 +268,21 @@ public class AudioHelper
manager.GetSessionEnumerator(out var sessionEnumerator); manager.GetSessionEnumerator(out var sessionEnumerator);
if(sessionEnumerator == null)
{
continue;
}
sessionEnumerator.GetCount(out var count); sessionEnumerator.GetCount(out var count);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
sessionEnumerator.GetSession(i, out var session); sessionEnumerator.GetSession(i, out var session);
if(session == null)
{
continue;
}
var volume = (ISimpleAudioVolume)session; var volume = (ISimpleAudioVolume)session;
var guid = Guid.Empty; var guid = Guid.Empty;
volume.SetMasterVolume(1, ref guid); volume.SetMasterVolume(1, ref guid);
@ -271,6 +301,10 @@ public class AudioHelper
{ {
deviceCollection.Item(d, out var device); deviceCollection.Item(d, out var device);
if(device == null)
{
continue;
}
Guid iid = typeof(IAudioSessionManager2).GUID; Guid iid = typeof(IAudioSessionManager2).GUID;
device.Activate(ref iid, CLSCTX.ALL, IntPtr.Zero, out var m); device.Activate(ref iid, CLSCTX.ALL, IntPtr.Zero, out var m);
@ -279,11 +313,21 @@ public class AudioHelper
manager.GetSessionEnumerator(out var sessionEnumerator); manager.GetSessionEnumerator(out var sessionEnumerator);
if(sessionEnumerator == null)
{
continue;
}
sessionEnumerator.GetCount(out var count); sessionEnumerator.GetCount(out var count);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
sessionEnumerator.GetSession(i, out var session); sessionEnumerator.GetSession(i, out var session);
if(session == null)
{
continue;
}
if (session.IsSystemSoundsSession() == 0) if (session.IsSystemSoundsSession() == 0)
{ {
return new SystemSoundsAudioSession(session); return new SystemSoundsAudioSession(session);
@ -388,6 +432,11 @@ public class AudioHelper
{ {
deviceCollection.Item(d, out var device); deviceCollection.Item(d, out var device);
if(device == null)
{
continue;
}
Guid iid = typeof(IAudioSessionManager2).GUID; Guid iid = typeof(IAudioSessionManager2).GUID;
device.Activate(ref iid, CLSCTX.ALL, IntPtr.Zero, out var m); device.Activate(ref iid, CLSCTX.ALL, IntPtr.Zero, out var m);
var manager = (IAudioSessionManager2)m; var manager = (IAudioSessionManager2)m;
@ -396,10 +445,21 @@ public class AudioHelper
manager.GetSessionEnumerator(out var sessionEnumerator); manager.GetSessionEnumerator(out var sessionEnumerator);
if(sessionEnumerator == null)
{
continue;
}
sessionEnumerator.GetCount(out var count); sessionEnumerator.GetCount(out var count);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
sessionEnumerator.GetSession(i, out var session); sessionEnumerator.GetSession(i, out var session);
if(session == null)
{
continue;
}
session.GetDisplayName(out var displayName); session.GetDisplayName(out var displayName);
session.GetProcessId(out var sessionProcessId); session.GetProcessId(out var sessionProcessId);

View File

@ -33,7 +33,7 @@
"Name": "Focused Application Volume", "Name": "Focused Application Volume",
"Description": "Control the volume of the focused application", "Description": "Control the volume of the focused application",
"URL": "https://github.com/dlprows/FocusVolumeControl", "URL": "https://github.com/dlprows/FocusVolumeControl",
"Version": "1.3.0", "Version": "1.3.1",
"CodePath": "FocusVolumeControl", "CodePath": "FocusVolumeControl",
"Category": "Volume Control [dlprows]", "Category": "Volume Control [dlprows]",
"Icon": "Images/pluginIcon", "Icon": "Images/pluginIcon",