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. }
public async Task <bool> OpenTrack(IPlaySource track, bool openCrossfading, long position) { IsLoading = true; if (!openCrossfading) { StopPlayback(); } if (_crossfadeService.IsFading) { _crossfadeService.Cancel(); } if (_soundSource != null && !openCrossfading) { _soundSource.Dispose(); } if (openCrossfading && _soundSource != null) { _soundOut.Stopped -= SoundOut_Stopped; _loopStream.StreamFinished -= LoopStream_StreamFinished; _simpleNotificationSource.BlockRead -= SimpleNotificationSource_BlockRead; _crossfadeService.CrossfadeOut(_soundOut, CrossfadeDuration).Forget(); _soundOut = null; } var tempSource = await GetSoundSource(track, position); if (tempSource == null) { return(false); } _soundSource = tempSource; if (_soundSource.WaveFormat.SampleRate < 44100) //Correct sample rate { _soundSource = _soundSource.ChangeSampleRate(44100); } _soundSource = _soundSource .AppendSource(x => new LoopStream(x), out _loopStream) .AppendSource(x => Equalizer.Create10BandEqualizer(x.ToSampleSource()), out _equalizer) .AppendSource(x => new SimpleNotificationSource(x) { Interval = 100 }, out _simpleNotificationSource) .ToWaveSource(); _loopStream.EnableLoop = IsLooping; _loopStream.StreamFinished += LoopStream_StreamFinished; _simpleNotificationSource.BlockRead += SimpleNotificationSource_BlockRead; for (var i = 0; i < EqualizerBands.Count; i++) { SetEqualizerBandValue(EqualizerBands.Bands[i].Value, i); } if (_soundOut == null) { _soundOut = _soundOutProvider.GetSoundOut(); _soundOut.Stopped += SoundOut_Stopped; } _soundOut.Initialize(_soundSource); _soundOut.Volume = Volume; IsLoading = false; OnTrackLengthChanged(); _playTimeStopwatch.Reset(); if (openCrossfading) { await TogglePlayPause(); _fadingService.FadeIn(_soundOut, Volume).Forget(); } CurrentStateChanged(); OnPositionChanged(); return(true); }