Exemplo n.º 1
0
        internal static AudioSessionModel GetAudioSessionModel(this IAudioSessionControl sControl, out bool isSystemSession)
        {
            if (sControl == null)
            {
                isSystemSession = false;
                return(null);
            }
            var     sessionControl = (IAudioSessionControl2)sControl;
            Process process;

            try
            {
                Marshal.ThrowExceptionForHR(sessionControl.GetProcessId(out var processId));
                process = Process.GetProcessById((int)processId);
            }
            catch (Exception e)
            {
                Logger.Error($"Failed to get session process", e);
                isSystemSession = false;
                return(null);
            }

            try
            {
                ImageSource iconImageSource = null;
                string      sessionName     = string.Empty;

                try
                {
                    sessionName = process.ProcessName;
                }
                catch { }

                if (sessionControl.IsNotSystemSounds())
                {
                    App.Current.Dispatcher.Invoke(() => iconImageSource = ExtractSessionIcon(process, sControl));
                    isSystemSession = false;
                }
                else
                {
                    isSystemSession = true;
                    //check if the system language was changed to get a new localized name for the system sounds session
                    var systemLanguage = TranslationSource.GetSystemLanguage();
                    if (SettingsProvider.Settings.SystemSoundsName == null ||
                        SettingsProvider.Settings.SystemLanguage != systemLanguage)
                    {
                        //returns resources "DllPath,index"
                        sessionName = sControl.GetSystemSoundsName();
                        SettingsProvider.Settings.SystemLanguage   = systemLanguage;
                        SettingsProvider.Settings.SystemSoundsName = sessionName;
                        SettingsProvider.SaveSettings().GetAwaiter().GetResult();
                    }
                    else
                    {
                        sessionName = SettingsProvider.Settings.SystemSoundsName;
                    }

                    try
                    {
                        App.Current.Dispatcher.Invoke(() =>
                        {
                            Icon icon =
                                Icon.ExtractAssociatedIcon(Environment.ExpandEnvironmentVariables("%SystemRoot%\\System32\\AudioSrv.dll"));
                            iconImageSource = IconHelper.GetImageSourceFromIcon(icon);
                        });
                    }
                    catch (Exception e)
                    {
                        Logger.Error($"Failed to extract system sounds icon from AudioSrv.dll.", e);
                    }
                }

                var sessionVolume = new AudioSessionVolume(sessionControl);
                var muteState     = sessionVolume.GetMute();
                var volume        = sessionVolume.GetVolume();
                sessionControl.GetSessionIdentifier(out var sessionId);

                AudioSessionStateNotifications sessionStateNotifications =
                    new AudioSessionStateNotifications(sessionControl);

                try
                {
                    sessionStateNotifications.RegisterNotifications();
                }
                catch (Exception e)
                {
                    Logger.Error($"Failed to register audio session notifications, process: [{sessionName}]", e);
                }

                AudioSessionModel session =
                    new AudioSessionModel(muteState, Convert.ToInt32(volume * 100), sessionName, sessionId, iconImageSource,
                                          sessionVolume,
                                          sessionStateNotifications);
                return(session);
            }
            catch (Exception e)
            {
                Logger.Error($"Failed to create audio session model, process: [{process.ProcessName}]", e);
            }
            isSystemSession = false;
            return(null);
        }