public void Play()
        {
            if (AudioFileReader == null)
            {
                lock (_locker)
                {
                    _loggerSoundPlayer.Info($"PlayFile In Play methode: AudioFileReader == null !!!!!!!!!!!!!!!!!!!!");
                }
                return;
            }

            try
            {
                if (WaveOutDevice.PlaybackState == PlaybackState.Paused ||
                    WaveOutDevice.PlaybackState == PlaybackState.Stopped)
                {
                    WaveOutDevice.Play();
                }
            }
            catch (Exception ex)
            {
                lock (_locker)
                {
                    _loggerSoundPlayer.Info($"PlayFile In Play methode: ECXEPTION {ex.Message} !!!!!!!!!!!!!!!!!!!!");
                }
                throw;
            }
        }
Esempio n. 2
0
 private void Play(Mp3FileReader provider)
 {
     LastPlayedTime = provider.TotalTime;
     provider.Seek(0, SeekOrigin.Begin);
     WaveOutDevice.Init(provider);
     WaveOutDevice.Play();
 }
Esempio n. 3
0
        static void Main()
        {
            using (var stream = new MemoryStream())
                using (var speechEngine = new SpeechSynthesizer())
                {
                    Console.WriteLine("Available devices:");
                    foreach (var device in WaveOutDevice.EnumerateDevices())
                    {
                        Console.WriteLine("{0}: {1}", device.DeviceId, device.Name);
                    }
                    Console.WriteLine("\nEnter device for speech output:");
                    var deviceId = (int)char.GetNumericValue(Console.ReadKey().KeyChar);

                    speechEngine.SetOutputToWaveStream(stream);
                    speechEngine.Speak("Testing 1 2 3");

                    using (var waveOut = new WaveOut {
                        Device = new WaveOutDevice(deviceId)
                    })
                        using (var waveSource = new MediaFoundationDecoder(stream))
                        {
                            waveOut.Initialize(waveSource);
                            waveOut.Play();
                            waveOut.WaitForStopped();
                        }
                }
        }
Esempio n. 4
0
 private static void AddWaveOutDevices(ICollection <Device> devices)
 {
     foreach (var directSoundDevice in WaveOutDevice.EnumerateDevices())
     {
         devices.Add(new Device(directSoundDevice.DeviceId.ToString(), directSoundDevice.Name, DeviceTypeEnum.Wave));
     }
 }
Esempio n. 5
0
        private void button3_Click(object sender, EventArgs e) //requires system.management .. but it works!
        {
            SpeechSynthesizer speechEngine = new SpeechSynthesizer();
            MemoryStream      stream       = new MemoryStream();

            //--- Make the voice a woman ---//
            var genderVoices = speechEngine.GetInstalledVoices().Where(arg => arg.VoiceInfo.Gender == VoiceGender.Female).ToList();
            var firstVoice   = genderVoices.FirstOrDefault();

            firstVoice = genderVoices.FirstOrDefault();
            if (firstVoice == null)
            {
                return;
            }
            speechEngine.SelectVoice(firstVoice.VoiceInfo.Name);
            //-------------------------------

            //Enumerate devices using WaveOutDevice from CSCore library (just enumeration basics)
            foreach (var device in WaveOutDevice.EnumerateDevices())
            {
                Console.WriteLine("{0}: {1}", device.DeviceId, device.Name);
            }
            Console.WriteLine("\nEnter device for speech output:");
            var deviceId = 2;//we have to select which one

            speechEngine.SetOutputToWaveStream(stream);
            speechEngine.Speak(textBox1.Text.ToString()); //we can get the contents of the text box and play it

            using (var waveOut = new WaveOut {
                Device = new WaveOutDevice(deviceId)
            })
                using (var waveSource = new MediaFoundationDecoder(stream))
                {
                    waveOut.Initialize(waveSource);
                    waveOut.Play();
                    waveOut.WaitForStopped();
                }
            //enumerate devices using Management objects (another way to do stuff, but with more details)

            /*
             * ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
             *      "SELECT * FROM Win32_SoundDevice");
             *
             * ManagementObjectCollection objCollection = objSearcher.Get();
             *
             * ManagementObject whatever = new ManagementObject();
             *
             * foreach (ManagementObject obj in objCollection)
             * {
             *  Console.Out.WriteLine("-------------------------------------");
             *  foreach (PropertyData property in obj.Properties)
             *  {
             *      Console.Out.WriteLine(String.Format("{0}:{1}", property.Name, property.Value));
             *      whatever = obj;
             *  }
             * }
             *
             */
        }
 public void Dispose()
 {
     if (WaveOutDevice != null)
     {
         WaveOutDevice.Stop();
         WaveOutDevice.Dispose();
     }
 }
Esempio n. 7
0
        public void CanCreatePlayer()
        {
            var device = new WaveOutDevice(0, new WaveOutCapabilities());

            device.Player.Should().Not.Be.Null();

            device.Dispose();
        }
Esempio n. 8
0
 public void GenerateDeviceList()
 {
     // Look for device to output sound to
     foreach (WaveOutDevice device in WaveOutDevice.EnumerateDevices())
     {
         deviceComboBox.Items.Add(device.Name);
     }
     deviceComboBox.SelectedIndex = 0;
 }
Esempio n. 9
0
        public void GetAudioDeviceTest()
        {
            int c = 0;

            foreach (WaveOutDevice d in WaveOutDevice.EnumerateDevices())
            {
                c++;
            }
            Assert.AreNotEqual(c, 0);
        }
        public List <string> GetWaveOutDeviceName()
        {
            List <CSCore.SoundOut.WaveOutDevice> list = new List <WaveOutDevice>();

            foreach (var dev in WaveOutDevice.EnumerateDevices())
            {
                list.Add(dev);
            }
            return(list.Select(x => "WaveOut - " + x.Name).ToList());
        }
Esempio n. 11
0
 public void Dispose()
 {
     channels.Clear();
     channels = null;
     if (WaveOutDevice != null)
     {
         WaveOutDevice.Stop();
         WaveOutDevice.Dispose();
     }
 }
Esempio n. 12
0
        private void Form1_Load(object sender, EventArgs e)
        {
            var mmdeviceEnumerator = WaveOutDevice.EnumerateDevices();

            foreach (var device in mmdeviceEnumerator)
            {
                _devices.Add(device);
            }

            comboBox1.DataSource    = _devices;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember   = "DeviceID";
        }
Esempio n. 13
0
        public void Open(string filename, WaveOutDevice device)
        {
            CleanupPlayback();

            _waveSource = new MediaFoundationDecoder(filename);
            _soundOut   = new WasapiOut()
            {
                Latency = 100
            };
            _soundOut.Initialize(_waveSource);
            if (PlaybackStopped != null)
            {
                _soundOut.Stopped += PlaybackStopped;
            }
        }
Esempio n. 14
0
        internal ISoundOut GetSoundOut()
        {
            switch (DeviceTypeEnum)
            {
            case DeviceTypeEnum.DirectSound:
                var directSoundOut = new DirectSoundOut();
                directSoundOut.Device = Guid.Parse(Id);
                return(directSoundOut);

            case DeviceTypeEnum.Wave:
                var waveOut = new WaveOut();
                waveOut.Device = WaveOutDevice.EnumerateDevices().FirstOrDefault(x => x.DeviceId == int.Parse(Id));
                return(waveOut);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public void Stop()
        {
            if (AudioFileReader == null)
            {
                return;
            }

            try
            {
                if (WaveOutDevice.PlaybackState == PlaybackState.Playing)
                {
                    WaveOutDevice.Stop();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public bool PlayFile(string file)
        {
            lock (_locker)
            {
                if (AudioFileReader != null)
                {
                    AudioFileReader.Dispose();
                    AudioFileReader = null;
                }

                try
                {
                    if (System.IO.File.Exists(file))
                    {
                        AudioFileReader = new AudioFileReader(file);

                        WaveOutDevice?.Stop();
                        WaveOutDevice?.Dispose();
                        WaveOutDevice = new WaveOut();

                        WaveOutDevice.Init(AudioFileReader);

                        SetVolume(0.9f);
                        Play();

                        return(true);
                    }

                    _loggerSoundPlayer.Info($"PlayFile In player: {file} FILE NOT FOUND ????????????????????");
                }
                catch (Exception ex)
                {
                    _loggerSoundPlayer.Info($"PlayFile In player: ECXEPTION {ex.Message} !!!!!!!!!!!!!!!!!!!!");
                }

                return(false);
            }
        }
        //public PlaybackStoppedDele PlaybackStopped { get; set; }

        //public PlaybackContiuneDele PlaybackContiune { set; get; }

        #endregion Properties

        #region Methods

        private void InitializePlayback(int Volume = 50, string openMethods = "waveout", string device = "扬声器")
        {
            MMDevice mMDevice;

            device      = device.Trim();
            openMethods = openMethods.Trim();
            if (openMethods.IndexOf("WaveOut") != -1)
            {
                IEnumerable <WaveOutDevice> dives     = WaveOutDevice.EnumerateDevices();
                IEnumerable <WaveOutDevice> divselect = dives.Where(x => x.Name.IndexOf(device) != -1);
                WaveOutDevice div = null;
                if (divselect.Count() == 0)
                {
                    div = dives.FirstOrDefault();
                }
                else if (divselect.Count() == 1)
                {
                    div = divselect.FirstOrDefault();
                }
                else
                {
                    Debug.Print("*****输入异常");
                    div = divselect.FirstOrDefault();
                }
                if (div == null)
                {
                    throw new NotSupportedException("not exist directsound device");
                }
                _soundOut = new WaveOut()
                {
                    Device = div, Latency = 100
                };                                                    //300延时有个运算溢出,怀疑是其他异常造成的
            }
            else if (openMethods.IndexOf("WasApiOut") != -1)
            {
                var enumerator = new MMDeviceEnumerator();
                IEnumerable <MMDevice> mMDevices = MMDeviceEnumerator.EnumerateDevices(DataFlow.Render).Where(x => x.DeviceState == DeviceState.Active);
                IEnumerable <MMDevice> dives     = enumerator.EnumAudioEndpoints(DataFlow.All, DeviceState.All).Where(x => x.DeviceState == DeviceState.Active);
                mMDevices = mMDevices.Join(dives, x => x.FriendlyName, x => x.FriendlyName, (x, y) => x).ToArray();
                mMDevice  = mMDevices.Where(x => x.FriendlyName.IndexOf(device) != -1).FirstOrDefault(x => x.DeviceState == DeviceState.Active);
                _soundOut = new WasapiOut()
                {
                    Device = mMDevice, Latency = 200
                };
            }
            else
            {
                IEnumerable <DirectSoundDevice> dives = DirectSoundDeviceEnumerator.EnumerateDevices();
                var divselect         = dives.Where(x => x.Description.IndexOf(device) != -1);
                DirectSoundDevice div = null;
                if (divselect.Count() == 0)
                {
                    div = dives.FirstOrDefault();
                }
                else if (divselect.Count() == 1)
                {
                    div = divselect.FirstOrDefault();
                }
                else
                {
                    //Debug.Print("*****输入异常*****");
                    div = divselect.FirstOrDefault();
                }
                if (div == null)
                {
                    throw new NotSupportedException("not exist directsound device");
                }
                _soundOut = new DirectSoundOut()
                {
                    Device = div.Guid, Latency = 100
                };
            }


            if (_filePath.LastIndexOf(".mp3") != -1)//流异步读取,此api异步读取flac流在频繁pos时有死锁bug
            {
                Stream fs = File.OpenRead(_filePath);
                _waveSource = new CSCore.Codecs.MP3.Mp3MediafoundationDecoder(fs);
            }
            else if (_filePath.LastIndexOf(".flac") != -1)
            {
                Stream fs = File.OpenRead(_filePath);
                _waveSource = new CSCore.Codecs.FLAC.FlacFile(fs, CSCore.Codecs.FLAC.FlacPreScanMode.Default);
                // _waveSource = new CSCore.Codecs.FLAC.FlacFile(_filePath);
            }
            else
            {
                _waveSource = CodecFactory.Instance.GetCodec(_filePath);
            }
            _soundOut.Initialize(_waveSource);
            _soundOut.Volume = Volume / 100f;

            //_soundOut.Stopped += _soundOut_Stopped;
            _total = _waveSource.GetLength();
        }
Esempio n. 18
0
        public static void Main()
        {
            GuiHandler gui    = new GuiHandler();
            var        handle = GetConsoleWindow();


            Console.WriteLine("Available devices:");
            foreach (WaveOutDevice device in WaveOutDevice.EnumerateDevices())
            {
                Console.WriteLine("{0}: {1}", device.DeviceId, device.Name);
            }

            Console.WriteLine("\nEnter device for speech output:");
            var deviceId = (int)char.GetNumericValue(Console.ReadKey().KeyChar);



            ShowWindow(handle, SW_HIDE);

            gui.CreateTaskBarIcon();
            gui.AddMenuOption("E&xit", (sender, e) => Environment.Exit(0));



            SpeechSynthesizer synth = new SpeechSynthesizer();

            synth.SelectVoiceByHints(VoiceGender.Female);

            foreach (InstalledVoice f in synth.GetInstalledVoices())
            {
                gui.AddMenuOption("Se&t voice to " + f.VoiceInfo.Name,
                                  (sender, e) => synth.SelectVoice(f.VoiceInfo.Name)
                                  );
            }



            new KeystrokeAPI().CreateKeyboardHook((character) =>
            {
                if ((int)character.KeyCode == 114)
                {
                    WaveOut waveOut = new WaveOut {
                        Device = new WaveOutDevice(deviceId)
                    };

                    MemoryStream stream = new MemoryStream();


                    synth.SetOutputToWaveStream(stream);

                    string text = GuiHandler.GetUserInput();

                    if (text != "")
                    {
                        synth.Speak(text);

                        var waveSource = new MediaFoundationDecoder(stream);
                        new Thread(() =>
                        {
                            waveOut.WaitForStopped();
                            waveOut.Initialize(waveSource);
                            waveOut.Play();
                            waveOut.WaitForStopped();
                        }).Start();
                    }
                }
            });

            Application.Run();
            while (true)
            {
            }
        }
Esempio n. 19
0
 public void Play()
 {
     WaveOutDevice.Init(AudioFileReader);
     IsPlaying = true;
     WaveOutDevice.Play();
 }
Esempio n. 20
0
 public void Pause()
 {
     WaveOutDevice.Stop();
     IsPlaying = false;
 }