public async Task PutSounds() { BrushConverter brushConverter = new BrushConverter(); string soundUrl = ""; foreach (var node in collection) { try { if (node.OuterHtml.Contains(".ogg")) { string baseString = Regex.Match(node.OuterHtml, @"https(.*)(ogg)").Value; int oggIndex = baseString.IndexOf("ogg"); soundUrl = baseString.Substring(0, oggIndex) + "ogg"; } else if (node.OuterHtml.Contains(".mp3")) { string baseString = Regex.Match(node.OuterHtml, @"https(.*)(mp3)").Value; int oggIndex = baseString.IndexOf("mp3"); soundUrl = baseString.Substring(0, oggIndex) + "mp3"; } else if (node.OuterHtml.Contains(".flac")) { string baseString = Regex.Match(node.OuterHtml, @"https(.*)(flac)").Value; int oggIndex = baseString.IndexOf("flac"); soundUrl = baseString.Substring(0, oggIndex) + "flac"; } string fileName = await DownloadSound(soundUrl); string filePath = $@"../../Sounds/{fileName}"; SoundButton button = new SoundButton(FileNameToLabel(fileName), filePath); button.Margin = new Thickness(5.0, 5.0, 10.0, 10.0); button.Background = (Brush)brushConverter.ConvertFrom("#FF4A2371"); button.Foreground = Brushes.White; button.Click += new RoutedEventHandler(ButtonClicked); buttonsPanel.Children.Add(button); } catch (Exception ex) { Debug.WriteLine($"Failure at {node.InnerText}"); } } }
private void ButtonClicked(object sender, RoutedEventArgs e) { SoundButton button = sender as SoundButton; WaveOutEvent waveOut = new WaveOutEvent(); try { if (button.SoundClip.Contains(".ogg")) { FileStream fileStream = new FileStream(button.SoundClip, FileMode.Open, FileAccess.Read); MemoryStream memoryStream = new MemoryStream(); fileStream.CopyTo(memoryStream); var vorbisStream = new NAudio.Vorbis.VorbisWaveReader(memoryStream); vorbisStream.Position = 0; waveOut.Init(vorbisStream); } else if (button.SoundClip.Contains(".mp3")) { var reader = new Mp3FileReader(button.SoundClip); reader.Position = 0; waveOut.Init(reader); } else if (button.SoundClip.Contains(".flac")) { var reader = new FlacReader(button.SoundClip); reader.Position = 0; waveOut.Init(reader); } waveOut.PlaybackStopped += new EventHandler <StoppedEventArgs>(AudioPlaybackStopped); waveOut.Play(); } catch (Exception ex) { Debug.WriteLine(ex); waveOut.Dispose(); } }