Beispiel #1
0
        public static async void PlayWavAsync(byte[] bytes)
        {
            try
            {
                using (MemoryStream stream = new MemoryStream(bytes))
                {
                    using (NAudio.Wave.WaveOutEvent waveOut = new NAudio.Wave.WaveOutEvent())
                    {
                        using (var reader = new NAudio.Wave.WaveFileReader(stream))
                        {
                            waveOut.Init(reader);
                            waveOut.Play();

                            while (waveOut.PlaybackState == NAudio.Wave.PlaybackState.Playing)
                            {
                                await Task.Delay(1);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.MainLogger.Log(Logger.LogTypes.Error, e.ToString());
            }
        }
Beispiel #2
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 });
            };
        }
Beispiel #3
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;
            }
            ;
        }
Beispiel #4
0
 private void stopped(object sender, EventArgs e)
 {
     NAudio.Wave.AudioFileReader reader = new NAudio.Wave.AudioFileReader(File);
     NAudio.Wave.WaveOutEvent    output = (NAudio.Wave.WaveOutEvent)sender;
     output.Init(reader);
     output.Play();
 }
Beispiel #5
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 });
            };
        }
Beispiel #6
0
 /// <summary>
 /// Playing an .ogg audio stream
 /// </summary>
 /// <param name="audioStream"></param>
 private static void PlayAudio(Stream audioStream)
 {
     using (var vorbisStream = new VorbisWaveReader(audioStream))
         using (var waveOut = new NAudio.Wave.WaveOutEvent())
         {
             waveOut.Init(vorbisStream);
             waveOut.Volume = 0.5f;
             waveOut.Play();
             Thread.Sleep(vorbisStream.TotalTime);
         }
 }
Beispiel #7
0
 private void DisposeWave()
 {
     if (output != null)
     {
         if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing)
         {
             output.Stop();
             output.Dispose();
             output = null;
         }
     }
 }
Beispiel #8
0
 public void Alert()
 {
     using (var audioFile = new NAudio.Wave.AudioFileReader(@"./Bensound-moose.wav"))
         using (var outputDevice = new NAudio.Wave.WaveOutEvent())
         {
             outputDevice.Init(audioFile);
             outputDevice.Play();
             while (outputDevice.PlaybackState == NAudio.Wave.PlaybackState.Playing)
             {
                 System.Threading.Thread.Sleep(1000);
             }
         }
 }
 private void initWaveOut()
 {
     if (this.waveOut != null)
     {
         this.waveOut.Dispose();
     }
     this.volumeWhenCached     = getBackgroundVolume();
     this.deviceIdWhenCached   = AudioPlayer.naudioBackgroundPlaybackDeviceId;
     this.waveOut              = new NAudio.Wave.WaveOutEvent();
     this.waveOut.DeviceNumber = this.deviceIdWhenCached;
     NAudio.Wave.SampleProviders.SampleChannel sampleChannel = new NAudio.Wave.SampleProviders.SampleChannel(reader);
     sampleChannel.Volume = this.volumeWhenCached;
     this.waveOut.Init(new NAudio.Wave.SampleProviders.SampleToWaveProvider(sampleChannel));
 }
Beispiel #10
0
 public void Play()
 {
     NAudio.Wave.AudioFileReader reader = new NAudio.Wave.AudioFileReader(File);
     NAudio.Wave.WaveOutEvent    output = new NAudio.Wave.WaveOutEvent
     {
         Volume = Volume
     };
     output.Init(reader);
     if (Looping)
     {
         output.PlaybackStopped += stopped;
     }
     output.Play();
 }
Beispiel #11
0
 private void Play(Stream stream)
 {
     mStop = false;
     Task.Factory.StartNew(() =>
     {
         using (var vorbisStream = new NAudio.Vorbis.VorbisWaveReader(stream))
         using (var waveOut = new NAudio.Wave.WaveOutEvent())
         {
             try
             {
                 waveOut.Init(vorbisStream);
                 waveOut.Play();
                 SpinWait.SpinUntil(() => vorbisStream.Position >= vorbisStream.Length || mStop);
                 Thread.Sleep(200);
             }
             catch (NAudio.MmException) { }
         }
     }).ContinueWith((t) => { if (OnPlayFinished != null) OnPlayFinished(); });
 }
Beispiel #12
0
 private void StopRecording()
 {
     btnRecord.Content = "Record";
     if (waveOut != null)
     {
         waveOut.Stop();
         waveOut.Dispose();
     }
     if (SourceStream != null)
     {
         SourceStream.StopRecording();
         SourceStream.Dispose();
     }
     if (waveWriter != null)
     {
         waveWriter.Dispose();
         waveWriter = null;
     }
     if (output != null)
     {
         if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing || output.PlaybackState == NAudio.Wave.PlaybackState.Paused)
         {
             output.Stop();
             output.Dispose();
             output = null;
         }
     }
     if (stopWatch.IsRunning)
     {
         stopWatch.Stop();
         currentTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                     0, 0, 0, 0);
         //txtBox.Text = currentTime;
     }
     else
     {
         currentTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                     0, 0, 0, 0);
         //txtBox.Text = currentTime;
     }
 }
Beispiel #13
0
 private void btnStop_Click(object sender, RoutedEventArgs e)
 {
     if (waveOut != null)
     {
         waveOut.Stop();
         waveOut.Dispose();
     }
     if (SourceStream != null)
     {
         SourceStream.StopRecording();
         SourceStream.Dispose();
     }
     if (waveWriter != null)
     {
         waveWriter.Dispose();
         waveWriter = null;
     }
     if (output != null)
     {
         if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing || output.PlaybackState == NAudio.Wave.PlaybackState.Paused)
         {
             output.Stop();
             output.Dispose();
             output = null;
         }
     }
     if (stopWatch.IsRunning)
     {
         stopWatch.Stop();
         currentTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                     0, 0, 0, 0);
         txtBox.Text = currentTime;
     }
     else
     {
         currentTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                     0, 0, 0, 0);
         txtBox.Text = currentTime;
     }
 }
Beispiel #14
0
        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Sound Files (*.mp3;*.wav)|*.mp3;*.wav;";
                open.ShowDialog();

                DisposeWave();
                //wave = new NAudio.Wave.WaveFileReader(open.FileName);
                if (open.FileName.EndsWith(".mp3"))
                {
                    NAudio.Wave.WaveStream pcm = new NAudio.Wave.Mp3FileReader(open.FileName);
                    stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
                }
                else if (open.FileName.EndsWith(".wav"))
                {
                    NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));
                    stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
                }

                output = new NAudio.Wave.WaveOutEvent();
                output.Init(new NAudio.Wave.WaveChannel32(stream));
                output.Play();
                StopWatch();
                if (stopWatch.IsRunning)
                {
                    stopWatch.Stop();
                }
                else
                {
                    stopWatch.Start();
                }

                btnPausePlay.IsEnabled = true;
            }catch (ArgumentException) {
                System.Windows.MessageBox.Show("Choose a real file");
            }
        }
Beispiel #15
0
        /// <summary>
        /// Converts .wem files to .ogg and plays them.
        /// </summary>
        /// <param name="file">The file to play.</param>
        public static void PlayAudio(string file)
        {
            file = ConvertToOgg(file);

            try
            {
                Task.Run(() => {
                    using (var vorbisStream = new NAudio.Vorbis.VorbisWaveReader(file))
                        using (var waveOut = new NAudio.Wave.WaveOutEvent())
                        {
                            waveOut.Init(vorbisStream);
                            waveOut.Play(); // is async
                            while (waveOut.PlaybackState != NAudio.Wave.PlaybackState.Stopped)
                            {
                                ;
                            }
                        }
                });
            }
            catch
            {
                GeneralHelper.WriteToConsole($"Problem playing audio file!!\n");
            }
        }
        public void DoPlay()
        {
            try
            {
                Console.WriteLine();
                Console.WriteLine("playing track. press space to play/pause; ESC to stop");
                System.Threading.Thread.Sleep(1000);

                NAudio.Wave.WaveOutEvent   wavout    = new NAudio.Wave.WaveOutEvent();
                NAudio.Wave.WaveFileReader wavreader = new NAudio.Wave.WaveFileReader(_beatFile.GetWavFilename());

                wavout.Init(wavreader);
                wavout.Play();

                int index = 0;
                List <BeatInstance> bl = _beatFile.GetAllBeats();

                while (wavout.PlaybackState != NAudio.Wave.PlaybackState.Stopped)
                {
                    if (System.Console.KeyAvailable)
                    {
                        switch (System.Console.ReadKey(true).Key)
                        {
                        case ConsoleKey.Spacebar:
                            if (wavout.PlaybackState == NAudio.Wave.PlaybackState.Paused)
                            {
                                wavout.Play();
                            }
                            else if (wavout.PlaybackState == NAudio.Wave.PlaybackState.Playing)
                            {
                                wavout.Pause();
                            }
                            break;

                        case ConsoleKey.Escape:
                            Console.WriteLine("stopped playback");
                            wavout.Stop();
                            break;

                        case ConsoleKey.OemPeriod:
                            Console.WriteLine("{0}", wavreader.CurrentTime.TotalSeconds);
                            break;
                        }
                    }

                    while (index < bl.Count && bl[index].TriggerTime <= wavreader.CurrentTime.TotalSeconds)
                    {
                        if (!_playQuiet)
                        {
                            ViewOneBeat(bl[index]);
                        }

                        index++;
                    }

                    System.Threading.Thread.Sleep(10);
                }

                wavout.Dispose();
                wavreader.Dispose();
            }
            catch (Exception ex)
            {
                throw new Exception("error playing track", ex);
            }
        }