Example #1
0
        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);
                    return false;
                }
            }

            return true;
        }
Example #2
0
        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.");
        }