コード例 #1
0
        public AudioDevice(Audio audio, AudioDeviceDescriptor deviceDescriptor)
        {
            _audio = audio;

            DeviceId         = deviceDescriptor.DeviceId;
            Name             = deviceDescriptor.Name;
            PlaybackSupport  = deviceDescriptor.PlaybackSupport;
            RecordingSupport = deviceDescriptor.RecordingSupport;

            _componentType = deviceDescriptor.PlaybackSupport
                                        ? deviceDescriptor.PlaybackComponentType
                                        : deviceDescriptor.RecordingComponentType;

            Open();

            DefaultLine.VolumeChanged += new Action <AudioLine>(DefaultLineVolumeChanged);
            DefaultLine.MuteChanged   += new Action <AudioLine>(DefaultLineMuteChanged);
        }
コード例 #2
0
ファイル: Audio.cs プロジェクト: modernstar/core
        private void ReloadAudioDevices()
        {
            if (_reloadDevicesInProgress)
            {
                return;
            }

            MMErrors errorCode = 0;
            IntPtr   hMixer    = IntPtr.Zero;

            try
            {
                _reloadDevicesInProgress = true;

                var       newAudioDevices = new List <AudioDeviceDescriptor>();
                MIXERCAPS wic             = new MIXERCAPS();

                int iNumDevs = MixerNative.mixerGetNumDevs();

                for (int i = 0; i < iNumDevs; i++)
                {
                    // Get info about the next device
                    errorCode = (MMErrors)MixerNative.mixerGetDevCaps(i, ref wic, Marshal.SizeOf(wic));
                    if (errorCode != MMErrors.MMSYSERR_NOERROR)
                    {
                        throw new MixerException(errorCode, GetErrorDescription(FuncName.fnMixerGetDevCaps, errorCode));
                    }

                    errorCode = (MMErrors)MixerNative.mixerOpen(out hMixer, i, IntPtr.Zero, IntPtr.Zero, 0);
                    if (errorCode != MMErrors.MMSYSERR_NOERROR)
                    {
                        throw new MixerException(errorCode, GetErrorDescription(FuncName.fnMixerOpen, errorCode));
                    }

                    MIXERLINE mxl = new MIXERLINE();
                    mxl.cbStruct = (uint)Marshal.SizeOf(mxl);

                    var deviceDescriptor = new AudioDeviceDescriptor()
                    {
                        DeviceId = i,
                        Name     = wic.szPname
                    };

                    foreach (var mixerlineComponenttype in AudioDevice.RecordingLineTypes)
                    {
                        mxl.dwComponentType = mixerlineComponenttype;
                        if (MixerNative.mixerGetLineInfo(hMixer, ref mxl, MIXER_GETLINEINFOF.COMPONENTTYPE) == 0)
                        {
                            deviceDescriptor.RecordingSupport       = true;
                            deviceDescriptor.RecordingComponentType = mixerlineComponenttype;

                            break;
                        }
                    }

                    foreach (var mixerlineComponenttype in AudioDevice.PlaybackLineTypes)
                    {
                        mxl.dwComponentType = mixerlineComponenttype;
                        if (MixerNative.mixerGetLineInfo(hMixer, ref mxl, MIXER_GETLINEINFOF.COMPONENTTYPE) == 0)
                        {
                            deviceDescriptor.PlaybackSupport       = true;
                            deviceDescriptor.PlaybackComponentType = mixerlineComponenttype;

                            break;
                        }
                    }

                    if (deviceDescriptor.PlaybackSupport || deviceDescriptor.RecordingSupport)
                    {
                        if (deviceDescriptor.PlaybackSupport && deviceDescriptor.RecordingSupport)
                        {
                            var playbackDeviceDescriptor = new AudioDeviceDescriptor()
                            {
                                DeviceId              = deviceDescriptor.DeviceId,
                                Name                  = deviceDescriptor.Name,
                                PlaybackSupport       = true,
                                RecordingSupport      = false,
                                PlaybackComponentType = deviceDescriptor.PlaybackComponentType
                            };

                            var recordingDeviceDescriptor = new AudioDeviceDescriptor()
                            {
                                DeviceId               = deviceDescriptor.DeviceId,
                                Name                   = deviceDescriptor.Name,
                                PlaybackSupport        = false,
                                RecordingSupport       = true,
                                RecordingComponentType = deviceDescriptor.RecordingComponentType
                            };

                            newAudioDevices.Add(playbackDeviceDescriptor);
                            newAudioDevices.Add(recordingDeviceDescriptor);
                        }
                        else
                        {
                            newAudioDevices.Add(deviceDescriptor);
                        }
                    }
                }

                if (newAudioDevices.Count > 0 || (_audioDevices.Count > 0 && iNumDevs == 0))
                {
                    UpdateAudioDevicesLists(newAudioDevices);
                }
            }
            finally
            {
                if (hMixer != IntPtr.Zero)
                {
                    MixerNative.mixerClose(hMixer);
                }

                _reloadDevicesInProgress = false;
            }
        }