public void CreateDevice(EventHandler <DefaultDeviceChangedEventArgs> aDefaultDeviceChangedHandler,
                                 EventHandler <AudioEndpointVolumeCallbackEventArgs> aVolumeChangedHandler)
        {
            try
            {
                //Create device and register default device change notification
                _mMdeviceEnumerator           = new MMDeviceEnumerator();
                iMultiMediaNotificationClient = new MMNotificationClient(_mMdeviceEnumerator);
                iMultiMediaNotificationClient.DefaultDeviceChanged += iDefaultDeviceChangedHandler = aDefaultDeviceChangedHandler;
                var mMdeviceList = _mMdeviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active);

                if (mMdeviceList != null && mMdeviceList.Count > 0)
                {
                    _mMdevice = _mMdeviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
                    GUIGraphicsContext.CurrentAudioRendererDevice = _mMdevice.FriendlyName;

                    //Register to get volume modifications
                    if (_mMdevice != null)
                    {
                        iAudioEndpointVolume = AudioEndpointVolume.FromDevice(_mMdevice);
                    }
                    iAudioEndpointVolumeMixerCallback = new CSCore.CoreAudioAPI.AudioEndpointVolumeCallback();
                    iAudioEndpointVolumeMixerCallback.NotifyRecived += iVolumeChangedHandler = aVolumeChangedHandler;
                    iAudioEndpointVolume?.RegisterControlChangeNotify(iAudioEndpointVolumeMixerCallback);
                }

                // For audio session
                Stop();
                //DispatchingTimerStart(); // Disable because the check will be done in IsMuted code
            }
            catch (Exception)
            {
                // When no device available
            }
        }
        public void Dispose()
        {
            // Client up our MM objects in reverse order
            if (iAudioEndpointVolumeMixerCallback != null && iAudioEndpointVolume != null)
            {
                iAudioEndpointVolume.UnregisterControlChangeNotify(iAudioEndpointVolumeMixerCallback);
            }

            if (iAudioEndpointVolumeMixerCallback != null)
            {
                iAudioEndpointVolumeMixerCallback.NotifyRecived -= iVolumeChangedHandler;
                iAudioEndpointVolumeMixerCallback = null;
            }

            if (iAudioEndpointVolume != null)
            {
                iAudioEndpointVolume.Dispose();
                iAudioEndpointVolume = null;
            }

            if (_mMdevice != null)
            {
                _mMdevice.Dispose();
                _mMdevice = null;
            }

            if (iMultiMediaNotificationClient != null)
            {
                iMultiMediaNotificationClient.DefaultDeviceChanged -= iDefaultDeviceChangedHandler;
                iMultiMediaNotificationClient.Dispose();
                iMultiMediaNotificationClient = null;
            }

            if (_mMdeviceEnumerator != null)
            {
                _mMdeviceEnumerator.Dispose();
                _mMdeviceEnumerator = null;
            }
            Stop();
            Close();
        }
Beispiel #3
0
        public void CanCreateAudioEndpointVolumeNotification()
        {
            using (var device = Utils.GetDefaultRenderDevice())
            using (var endpointVolume = AudioEndpointVolume.FromDevice(device))
            {
                AudioEndpointVolumeCallback callback = new AudioEndpointVolumeCallback();
                callback.NotifyRecived += (s, e) =>
                {
                    Debug.WriteLine("Notification1");
                    //Debug.Assert(e.Channels == endpointVolume.ChannelCount);
                };
                endpointVolume.RegisterControlChangeNotify(callback);

                var vol = endpointVolume.GetChannelVolumeLevelScalar(0);
                endpointVolume.SetChannelVolumeLevelScalar(0, 1f, Guid.Empty);
                endpointVolume.SetChannelVolumeLevelScalar(0, vol, Guid.Empty);

                endpointVolume.UnregisterControlChangeNotify(callback);
                System.Threading.Thread.Sleep(1000);
            }
        }