示例#1
0
        /// <inheritdoc/>
        public async Task ControlAsync(IProcessSelectionCriteria criteria, MultimediaAction action)
        {
            var manager = await this.GetManagerAsync();

            var predicate = criteria.ToPredicate();

            foreach (var session in manager.GetSessions().Where(predicate.IsMatch))
            {
                await(action switch
                {
                    MultimediaAction.Play => session.TryPlayAsync(),
                    MultimediaAction.Pause => session.TryPauseAsync(),
                    MultimediaAction.Stop => session.TryStopAsync(),
                    MultimediaAction.SkipPrevious => session.TrySkipPreviousAsync(),
                    MultimediaAction.SkipNext => session.TrySkipNextAsync(),
                    _ => session.TryTogglePlayPauseAsync()
                });
示例#2
0
        /// <summary>
        /// Converts this instance to a <see cref="ISessionPredicate"/>.
        /// </summary>
        /// <param name="criteria">The criteria; this instance.</param>
        /// <returns>The <see cref="ISessionPredicate"/>.</returns>
        /// <exception cref="ArgumentNullException">Cannot select process as process name has not been specified.</exception>
        public static ISessionPredicate ToPredicate(this IProcessSelectionCriteria criteria)
        {
            // Select the process by name.
            if (criteria.ProcessSelectionType == ProcessSelectionType.ByName)
            {
                if (string.IsNullOrWhiteSpace(criteria.ProcessName))
                {
                    throw new ArgumentNullException("Cannot select process as process name has not been specified");
                }

                return(new ProcessNamePredicate(criteria.ProcessName));
            }

            // Select the foreground process predicate.
            var hwnd = User32.GetForegroundWindow();

            User32.GetWindowThreadProcessId(hwnd, out var processId);

            return(new ProcessIdentifierPredicate(processId));
        }
示例#3
0
        /// <inheritdoc/>
        public void SetDefaultAudioDevice(IProcessSelectionCriteria criteria, string deviceKey)
        {
            var dataFlow  = this.GetDataFlow(deviceKey);
            var predicate = criteria.ToPredicate();

            foreach (var audioSession in this.GetAudioSessions().Where(predicate.IsMatch))
            {
                // Default to zero pointer; this will only change if an audio device has been specified.
                var hstring = IntPtr.Zero;
                var device  = AudioDevices.Current.GetDeviceByKey(deviceKey);
                if (!device.IsDynamic)
                {
                    var persistDeviceId = this.GenerateDeviceId(device.Id, dataFlow);
                    Combase.WindowsCreateString(persistDeviceId, (uint)persistDeviceId.Length, out hstring);
                }

                // Set the audio device for the process.
                var audioSessionProcessId = audioSession.GetProcessID;
                this.AudioPolicyConfig.SetPersistedDefaultAudioEndpoint(audioSessionProcessId, dataFlow, Role.Console, hstring);
                this.AudioPolicyConfig.SetPersistedDefaultAudioEndpoint(audioSessionProcessId, dataFlow, Role.Multimedia, hstring);
                this.AudioPolicyConfig.SetPersistedDefaultAudioEndpoint(audioSessionProcessId, dataFlow, Role.Communications, hstring);
            }
        }