// ******************************** Audio Devices - System Audio Notification Client EventHandler #region Audio Devices - System Audio Notification Client EventHandler // handles the events from the System Audio Notification Client (one client for all Players) for this player only // gets the main ui thread for the events (so the end user doesn't have to use "InvokeRequired") // or should the "ISynchronizeInvoke" be used instead of the "control" method used here? internal void SystemAudioDevicesChanged(object sender, SystemAudioDevicesEventArgs e) { if (_mediaSystemAudioDevicesChanged != null) { Control control = _display; if (control == null) { FormCollection forms = Application.OpenForms; if (forms != null && forms.Count > 0) { control = forms[0]; } } if (control != null) { OnSystemDevicesChangedDelegate d = new OnSystemDevicesChangedDelegate(OnSystemAudioDevicesChanged); control.BeginInvoke(d, new object[] { e }); // what's the best? //control.Invoke(new Action<SystemAudioDevicesEventArgs>(RaiseDeviceChangedEvent), new object[] { e }); //control.Invoke((MethodInvoker)delegate { RaiseDeviceChangedEvent(e); }); } else { // no ui thread found, just fire away... _mediaSystemAudioDevicesChanged(this, e); } } }
internal static void AudioDevicesClientClose() { if (pm_AudioDevicesCallback != null) { try { IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); deviceEnumerator.UnregisterEndpointNotificationCallback(pm_AudioDevicesCallback); pm_AudioDevicesCallback = null; pm_AudioDevicesEventArgs = null; if (deviceEnumerator != null) { Marshal.ReleaseComObject(deviceEnumerator); deviceEnumerator = null; } } catch { /* ignore */ } } }
internal static bool AudioDevicesClientOpen() { bool result = true; if (pm_AudioDevicesCallback == null) { try { IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); pm_AudioDevicesCallback = new AudioDevicesClient(); deviceEnumerator.RegisterEndpointNotificationCallback(pm_AudioDevicesCallback); pm_AudioDevicesEventArgs = new SystemAudioDevicesEventArgs(); if (deviceEnumerator != null) { Marshal.ReleaseComObject(deviceEnumerator); } } catch { result = false; } } return(result); }
private void SystemDevicesChangedHandler(object sender, SystemAudioDevicesEventArgs e) { if (e.IsInputDevice) { return; } IMMDeviceCollection deviceCollection; uint count; IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator(); deviceEnumerator.EnumAudioEndpoints(EDataFlow.eRender, (uint)DeviceState.Active, out deviceCollection); deviceCollection.GetCount(out count); Marshal.ReleaseComObject(deviceEnumerator); if (deviceCollection != null) { Marshal.ReleaseComObject(deviceCollection); } if (e._notification == SystemAudioDevicesNotification.Activated) { if (count == 1) { if (_playing && !_paused) { // cannot resume playback //if (pm_HasPeakMeter) //{ // PeakMeter_Open(_audioDevice, true); //} // maybe this Control control = _display; if (control == null) { FormCollection forms = Application.OpenForms; if (forms != null && forms.Count > 0) { control = forms[0]; } } if (control != null) { control.BeginInvoke(new MethodInvoker(delegate { AV_UpdateTopology(); })); } } if (_mediaAudioDeviceChanged != null) { _mediaAudioDeviceChanged(this, EventArgs.Empty); } } } else if (_audioDevice == null) { if (pm_HasPeakMeter && e._notification == SystemAudioDevicesNotification.DefaultChanged) { if (count != 0) { PeakMeter_Open(_audioDevice, true); } else { pm_PeakMeterChannelCount = 0; } if (_mediaAudioDeviceChanged != null) { _mediaAudioDeviceChanged(this, EventArgs.Empty); } } } else { if (e._deviceId == _audioDevice.Id && (e._notification == SystemAudioDevicesNotification.Removed || e._notification == SystemAudioDevicesNotification.Disabled)) { if (count != 0) { _audioDevice = null; if (pm_HasPeakMeter) { PeakMeter_Open(_audioDevice, true); } } else { pm_PeakMeterChannelCount = 0; } if (_mediaAudioDeviceChanged != null) { _mediaAudioDeviceChanged(this, EventArgs.Empty); } } } }
// raise the SystemAudioDevicesChanged event on the player's main ui thread private void OnSystemAudioDevicesChanged(SystemAudioDevicesEventArgs e) { _mediaSystemAudioDevicesChanged(this, e); }