示例#1
0
        private bool HandleStreamSwitchEvent()
        {
            Debug.Assert(_inStreamSwitch);

            //Step 1: Stop rendering
            _audioClient.Stop();

            //Step 3: Wait for the default device to change.
            if (!_streamSwitchCompleteEvent.WaitOne(750))
            {
                Debug.WriteLine("Stream switch timeout - aborting...");
                goto StreamSwitchErrorExit;
            }

            //Step 2: Release resources.
            _audioSessionControl.UnregisterAudioSessionNotification(_eventHandler);

            CSCoreUtils.SafeRelease(ref _audioSessionControl);
            CleanupResources(true);
            CSCoreUtils.SafeRelease(ref _device);

            //Step 4: Try to get new endpoint
            try
            {
                _deviceEnumerator = _deviceEnumerator ?? new MMDeviceEnumerator();
                _device           = _deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, DeviceRole);
            }
            catch (CoreAudioAPIException exception)
            {
                Debug.WriteLine("Unable to retrieve new default device during stream switch: 0x" + exception.ErrorCode.ToString("x8"));
                goto StreamSwitchErrorExit;
            }

            //Step 5: Re-instantiate the audio client on the new endpoint.
            InitializeInternal();

            _audioClient.Start();

            _inStreamSwitch = false;
            return(true);

StreamSwitchErrorExit:
            _inStreamSwitch = false;
            return(false);
        }
示例#2
0
        private void TerminateStreamRouting()
        {
            if (_audioSessionControl != null)
            {
                _audioSessionControl.UnregisterAudioSessionNotification(_eventHandler);
                CSCoreUtils.SafeRelease(ref _audioSessionControl);
            }

            if (_deviceEnumerator != null)
            {
                _deviceEnumerator.UnregisterEndpointNotificationCallback(_eventHandler);
                CSCoreUtils.SafeRelease(ref _deviceEnumerator);
            }

            if (_streamSwitchCompleteEvent != null)
            {
                _streamSwitchCompleteEvent.Close();
                _streamSwitchCompleteEvent = null;
            }
        }