private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (_dsOne != null) { afrOne.Dispose(); loopOne.Dispose(); _dsOne.Dispose(); } if (_dsTwo != null) { afrTwo.Dispose(); loopTwo.Dispose(); _dsTwo.Dispose(); } if (driverOut != null) { driverOut.Dispose(); } //if (_WaveOutEventOne != null) //{ // _WaveOutEventOne.Dispose(); // _WaveOutEventOne = null; //} //if (_WaveOutEventTwo != null) //{ // _WaveOutEventTwo.Dispose(); // _WaveOutEventTwo = null; //} }
public void Dispose() { mp3Reader?.Close(); mp3Reader?.Dispose(); loopStream?.Close(); loopStream?.Dispose(); waveOut?.Dispose(); }
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); } }
protected virtual void Dispose(bool disposing) { if (disposing) { // free managed resources if (_fadingService.IsFading) { _fadingService.Cancel(); } StopPlayback(); _soundOut?.Dispose(); _soundOut = null; _soundSource?.Dispose(); SoundOutProvider.Dispose(); _loopStream?.Dispose(); _equalizer?.Dispose(); _simpleNotificationSource?.Dispose(); _soundSourceLoadingToken?.Dispose(); } // free native resources if there are any. }