private async void TryFind() { SearchResponse searchResult = await _mobileClient.SearchAsync(_currentTrackInfo.metadata); _trackSearchResult.Clear(); _mainWindow.SearchResultListBox.Items.Clear(); foreach (SearchResult resultEntry in searchResult.Entries) { if (resultEntry.Track == null) { continue; } _trackSearchResult.Add(resultEntry.Track); var item = new ListBoxItem() { Content = resultEntry.Track }; _mainWindow.SearchResultListBox.Items.Add(item); } }
private async Task MusicCommand(Channel channel, string query) { await channel.SendIsTyping(); await this.InitVoice(); if (this.readingMessages) { await channel.SendMessage("I'm busy reading shit."); return; } if (this.playingMusic) { await channel.SendMessage("No, I like this song."); return; } this.playingMusic = true; var mc = new MobileClient(); if (await mc.LoginAsync(this.config.GoogleUsername, this.config.GooglePassword)) { var result = await mc.SearchAsync(query); var entry = result.Entries.Where(x => x.Track != null).FirstOrDefault(); if (entry != null) { try { var track = entry.Track; var uri = await mc.GetStreamUrlAsync(track); var request = WebRequest.CreateHttp(uri); using (var rsp = request.GetResponse()) using (var web = rsp.GetResponseStream()) using (var mem = new MemoryStream()) { int count; await channel.SendMessage("Buffering song..."); var channels = this.discord.GetService <AudioService>().Config.Channels; var format = new WaveFormat(48000, 16, channels); var buffer = new byte[format.AverageBytesPerSecond / 50]; while (this.playingMusic && (count = web.Read(buffer, 0, buffer.Length)) > 0) { mem.Write(buffer, 0, count); } mem.Seek(0, SeekOrigin.Begin); await channel.SendMessage($"\r\n{track.Artist}\r\n*{track.Album}*\r\n**{track.Title}**"); using (var mp3 = new Mp3FileReader(mem)) using (var resampler = new MediaFoundationResampler(mp3, format)) { resampler.ResamplerQuality = 60; buffer = new byte[format.AverageBytesPerSecond / 50]; while (this.playingMusic && (count = resampler.Read(buffer, 0, buffer.Length)) > 0) { this.audio.Send(buffer, 0, count); } } } } catch (Exception) { await channel.SendMessage($"Guess THIS is the day that the music died."); } this.playingMusic = false; } else { this.playingMusic = false; await channel.SendMessage("Sorry, never heard of that song."); } } else { this.playingMusic = false; await channel.SendMessage("Uhm, maybe config me better? kthx"); } }