Esempio n. 1
0
        public bool SetAudioEndpoint(string dev, bool usedefault = false)
        {
            System.Collections.ObjectModel.ReadOnlyCollection <DirectSoundDevice> list = DirectSoundDeviceEnumerator.EnumerateDevices();

            DirectSoundDevice dsd = null;

            if (dev != null)                                               // active selection
            {
                dsd = list.FirstOrDefault(x => x.Description.Equals(dev)); // find

                if (dsd == null && !usedefault)                            // if not found, and don't use the default (used by constructor)
                {
                    return(false);
                }
            }

            DirectSoundOut dso = new DirectSoundOut(200, System.Threading.ThreadPriority.Highest); // seems good quality at 200 ms latency

            if (dso == null)                                                                       // if no DSO, fail..
            {
                return(false);
            }

            if (dsd != null)
            {
                dso.Device = dsd.Guid;
            }
            else
            {
                DirectSoundDevice def = DirectSoundDevice.DefaultDevice;
                dso.Device = def.Guid;  // use default GUID
            }

            NullWaveSource nullw = new NullWaveSource(10);

            try
            {
                dso.Initialize(nullw);  // check it takes it.. may not if no sound devices there..
                dso.Stop();
                nullw.Dispose();
            }
            catch
            {
                nullw.Dispose();
                dso.Dispose();
                return(false);
            }

            if (aout != null)                 // clean up last
            {
                aout.Stopped -= Output_Stopped;
                aout.Stop();
                aout.Dispose();
            }

            aout          = dso;
            aout.Stopped += Output_Stopped;

            return(true);
        }