Exemplo n.º 1
0
        /// <summary>
        /// Initializes the audio capture device.
        /// </summary>
        /// <param name="deviceDescription">
        /// The friendly name description of the device to capture from. This is usually
        /// something like "Microphone Array (USB Audio)". To capture from
        /// the default device, pass in NULL or an empty string.
        /// </param>
        public void Initialize(string deviceDescription)
        {
            // Activate native audio COM objects on a thread-pool thread to ensure that they are in an MTA
            Task.Run(() =>
            {
                if (string.IsNullOrEmpty(deviceDescription))
                {
                    // use the default console device
                    this.audioDevice = DeviceUtil.GetDefaultDevice(EDataFlow.Capture, ERole.Console);
                }
                else
                {
                    this.audioDevice = DeviceUtil.GetDeviceByName(EDataFlow.Capture, deviceDescription);
                }

                if (this.audioDevice != null)
                {
                    // Try to get the volume control
                    object obj  = this.audioDevice.Activate(new Guid(Guids.IAudioEndpointVolumeIIDString), ClsCtx.ALL, IntPtr.Zero);
                    this.volume = (IAudioEndpointVolume)obj;

                    // Now create an IAudioEndpointVolumeCallback object that wraps the callback and register it with the endpoint.
                    this.volumeCallback = new AudioEndpointVolumeCallback(this.AudioVolumeCallback);
                    this.volume.RegisterControlChangeNotify(this.volumeCallback);
                }
            }).Wait();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes the audio capture device.
        /// </summary>
        /// <param name="deviceDescription">
        /// The friendly name description of the device to capture from. This is usually
        /// something like "Microphone Array (USB Audio)". To capture from
        /// the default device, pass in NULL or an empty string.
        /// </param>
        public void Initialize(string deviceDescription)
        {
            Exception taskException = null;

            // Activate native audio COM objects on a thread-pool thread to ensure that they are in an MTA
            Task.Run(() =>
            {
                try
                {
                    if (string.IsNullOrEmpty(deviceDescription))
                    {
                        // use the default console device
                        this.audioDevice = DeviceUtil.GetDefaultDevice(EDataFlow.Capture, ERole.Console);
                    }
                    else
                    {
                        this.audioDevice = DeviceUtil.GetDeviceByName(EDataFlow.Capture, deviceDescription);
                    }

                    if (this.audioDevice != null)
                    {
                        // Try to get the volume control
                        object obj  = this.audioDevice.Activate(new Guid(Guids.IAudioEndpointVolumeIIDString), ClsCtx.ALL, IntPtr.Zero);
                        this.volume = (IAudioEndpointVolume)obj;

                        // Now create an IAudioEndpointVolumeCallback object that wraps the callback and register it with the endpoint.
                        this.volumeCallback = new AudioEndpointVolumeCallback(this.AudioVolumeCallback);
                        this.volume.RegisterControlChangeNotify(this.volumeCallback);
                    }
                }
                catch (Exception e)
                {
                    taskException = e;
                }
            }).Wait();

            // do error checking on the main thread
            if (taskException != null)
            {
                // rethrow exception
                throw taskException;
            }
            else if (this.audioDevice == null)
            {
                throw new IOException(string.IsNullOrEmpty(deviceDescription) ?
                                      "No default audio capture device found." :
                                      $"Audio capture device {deviceDescription} not found.");
            }
        }