Beispiel #1
0
        public static async void PlaySound(string sound, bool loop)
        {
            Logger.WriteLine($"attempting to play sound: {sound}");

            try
            {
                // gc isn't happy rn
                // create wave out event and initialize it with the vorbis wave reader
                using (WaveOutEvent waveOut = new WaveOutEvent())
                {
                    // create vorbis wave reader
                    using (VorbisWaveReader v = new VorbisWaveReader($"{Static.audioPath}\\{sound}.ogg"))
                    {
                        // also create a loop stream and initialize the wave out event with the loop stream instead of loop is true
                        using (LoopStream loopStream = new LoopStream(v))
                        {
                            if (loop)
                            {
                                waveOut.Init(loopStream);
                            }
                            else
                            {
                                waveOut.Init(v);
                            }

                            // flush and dispose the streams after playback stops
                            void Dispose(object sender, StoppedEventArgs e)
                            {
                                v.Flush();
                                v.Dispose();
                                waveOut.Dispose();
                                loopStream.Flush();
                                loopStream.Dispose();
                            }
                            waveOut.PlaybackStopped += Dispose;

                            // play
                            waveOut.Play();

                            // add the wave out event to the active audio list so it can be stopped manually
                            activeAudio.Add(waveOut);

                            // wait the duration of the sound
                            await Task.Delay(v.TotalTime);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionMessage.New(ex, true);
            }
        }