public static Volume GetInstance() { var volume = new Volume(); var deviceEnumeratorType = Type.GetTypeFromCLSID(new Guid(ComCLSIDs.MMDeviceEnumeratorCLSID)); var devenum = (IMMDeviceEnumerator)Activator.CreateInstance(deviceEnumeratorType); IMMDevice device; devenum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out device).ThrowIfError(); object objSessionManager; device.Activate(new Guid(ComIIDs.IAudioSessionManager2IID), (uint)CLSCTX.CLSCTX_INPROC_SERVER, IntPtr.Zero, out objSessionManager).ThrowIfError(); var sessionManager = objSessionManager as IAudioSessionManager2; if (sessionManager == null) throw new Exception("Session is not found."); IAudioSessionEnumerator sessions; sessionManager.GetSessionEnumerator(out sessions).ThrowIfError(); // sessionID は空にするとデフォルトセッションが取れるらしい ISimpleAudioVolume simpleAudioVolume; sessionManager.GetSimpleAudioVolume(Guid.Empty, 0, out simpleAudioVolume).ThrowIfError(); volume.simpleAudioVolume = simpleAudioVolume; simpleAudioVolume.GetMute(out volume._IsMute).ThrowIfError(); // sessionControl のインスタンスは取っておかないと通知来なくなる sessionManager.GetAudioSessionControl(Guid.Empty, 0, out volume.sessionControl).ThrowIfError(); volume.sessionControl.RegisterAudioSessionNotification(volume).ThrowIfError(); return volume; }
private bool CreateVolumeInstanceIfNull() { if (this.volume == null) { try { this.volume = Volume.GetInstance(); this.CompositeDisposable.Add(new PropertyChangedEventListener(this.volume) { { nameof(this.volume.IsMute), (sender, args) => this.IsMute = this.volume.IsMute }, }); this.IsMute = this.volume.IsMute; } catch (Exception ex) { Debug.WriteLine(ex); return false; } } return true; }
private bool CreateVolumeInstanceIfNull() { if (this.volume == null) { try { this.volume = Volume.GetInstance(); this.CompositeDisposable.Add(new PropertyChangedEventListener(this.volume) { { "IsMute", (sender, args) => this.IsMute = this.volume.IsMute }, }); this.IsMute = this.volume.IsMute; } catch (Exception ex) { Debug.WriteLine(ex); KanColleClient.Current.CatchedErrorLogWriter.ReportException(ex.Source, ex); return false; } } return true; }
public static Volume GetInstance() { var volume = new Volume(); var processId = Process.GetCurrentProcess().Id; var devenum = new MMDeviceEnumerator(); var device = devenum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); for (var i = 0; i < device.AudioSessionManager.Sessions.Count; i++) { var session = device.AudioSessionManager.Sessions[i]; if (session.ProcessID == processId) { volume.simpleAudioVolume = session.SimpleAudioVolume; volume.IsMute = session.SimpleAudioVolume.Mute; volume.Value = (int)(session.SimpleAudioVolume.MasterVolume * 100); // ToDo: ↓ これ入れて通知受けるようにすると、通知が走ったタイミングでアプリが落ちる。意味不明。誰か助けて。 //session.RegisterAudioSessionNotification(volume); return volume; } } throw new Exception("Session is not found."); }