Exemplo n.º 1
0
        public void PlayFile(string filename, bool wait)
        {
            FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);

            if (wait)
            {
                NAudio.Wave.WaveFileReader wfr = new NAudio.Wave.WaveFileReader(stream);
                NAudio.Wave.WaveOutEvent   wo  = new NAudio.Wave.WaveOutEvent();
                wo.DeviceNumber = dev;
                if (vol != -1)
                {
                    wo.Volume = vol;
                }
                wo.Init(wfr);
                wo.Play();
                while (wo.PlaybackState == NAudio.Wave.PlaybackState.Playing)
                {
                    System.Threading.Thread.Sleep(10);
                }
                wfr.Close();
                stream.Close();
            }
            else
            {
                System.Threading.Thread thr = new System.Threading.Thread(Play);
                thr.Start(new object[] { stream, true });
            };
        }
Exemplo n.º 2
0
        private void Play(object stream_close)
        {
            object[] strmcls     = (object[])stream_close;
            Stream   stream      = (Stream)strmcls[0];
            bool     closeStream = (bool)strmcls[1];

            NAudio.Wave.WaveFileReader wfr = new NAudio.Wave.WaveFileReader(stream);
            NAudio.Wave.WaveOutEvent   wo  = new NAudio.Wave.WaveOutEvent();
            wo.DeviceNumber = dev;
            if (vol != -1)
            {
                wo.Volume = vol;
            }
            wo.Init(wfr);
            wo.Play();
            while (wo.PlaybackState == NAudio.Wave.PlaybackState.Playing)
            {
                System.Threading.Thread.Sleep(10);
            }
            if (closeStream)
            {
                wfr.Close();
                stream.Close();
                stream = null;
            }
            ;
        }
Exemplo n.º 3
0
        public void PlayStream(Stream stream, bool wait, bool closeStream)
        {
            stream.Position = 0;

            if (wait)
            {
                NAudio.Wave.WaveFileReader wfr = new NAudio.Wave.WaveFileReader(stream);
                NAudio.Wave.WaveOutEvent   wo  = new NAudio.Wave.WaveOutEvent();
                wo.DeviceNumber = dev;
                if (vol != -1)
                {
                    wo.Volume = vol;
                }
                wo.Init(wfr);
                wo.Play();
                while (wo.PlaybackState == NAudio.Wave.PlaybackState.Playing)
                {
                    System.Threading.Thread.Sleep(10);
                }
                if (closeStream)
                {
                    wfr.Close();
                    stream.Close();
                    stream = null;
                }
                ;
            }
            else
            {
                System.Threading.Thread thr = new System.Threading.Thread(Play);
                thr.Start(new object[] { stream, closeStream });
            };
        }
Exemplo n.º 4
0
        private async static void PlaySoundWav(MessageEventArgs e)
        {
            if (e.Message.Text.Length > "-play ".Length && voiceclient != null)
            {
                string file = e.Message.Text.Substring("-play ".Length);
                Console.WriteLine("Trying to play: " + file);
                try {
                    var ws = new NAudio.Wave.WaveFileReader(file);
                    byte[] buf;
                    if (ws.WaveFormat.Channels > 1)
                    {
                        var tomono = new NAudio.Wave.StereoToMonoProvider16(ws);
                        tomono.RightVolume = 0.5f;
                        tomono.LeftVolume = 0.5f;
                        buf = new byte[ws.Length];
                        while (ws.HasData(ws.WaveFormat.AverageBytesPerSecond))
                        {
                            tomono.Read(buf, 0, ws.WaveFormat.AverageBytesPerSecond);
                            voiceclient.SendVoicePCM(buf, buf.Length);
                        }
                    }
                    else
                    {
                        buf = new byte[ws.Length];
                        ws.Read(buf, 0, buf.Length);
                        voiceclient.SendVoicePCM(buf, buf.Length);
                    }
                    ws.Close();
                }
                catch(Exception)
                {
                    Console.WriteLine("File not found or incompatible.");
                }

            }
        }
Exemplo n.º 5
0
 private void Play_TTS_file(string filename)
 {
     Console.WriteLine("In Play_TTS_file start");
     NAudio.Wave.WaveFileReader audio = new NAudio.Wave.WaveFileReader(filename);
     NAudio.Wave.IWavePlayer player = new NAudio.Wave.WaveOut(NAudio.Wave.WaveCallbackInfo.FunctionCallback());
     player.Init(audio);
     player.Play();
     while (true)
     {
         if (player.PlaybackState == NAudio.Wave.PlaybackState.Stopped)
         {
             player.Dispose();
             //MessageBox.Show("disposed");
             audio.Close();
             audio.Dispose();
             break;
         }
     };
     Console.WriteLine("After Play_TTS_File while loop");
 }