public async Task GetStreamUrl() { var account = CommonTests.GetAccount(); var mc = new MobileClient(); Assert.IsTrue(await mc.LoginAsync(account.Item1, account.Item2)); RadioFeed tracks; Assert.IsNotNull(tracks = await mc.GetStationFeed(ExplicitType.Explicit, new StationFeedStation { LibraryContentOnly = false, NumberOfEntries = 25, RecentlyPlayed = new Track[0], Seed = new StationSeed { SeedType = 6 } } )); var track = tracks.Data.Stations.First().Tracks.First(); Assert.IsNotNull(await mc.GetStreamUrlAsync(track)); }
public static async void PlayTrack(MobileClient client, Track track) { if (track == null || client == null) { return; } var streamUrl = await client.GetStreamUrlAsync(track); if (streamUrl == null) { return; } _currentTrack = track; PlayTrack(streamUrl); }
private async void OnPlayTrackBtnClicked() { if (!_isPlayingNow) { var url = await _mobileClient.GetStreamUrlAsync(_selectedTrack); var mediaFoundationReader = new MediaFoundationReader(url.ToString()); _waveOutEvent.Init(mediaFoundationReader); _waveOutEvent.Play(); _isPlayingNow = true; _mainWindow.PlayTrackBtn.Content = "IsPlaying"; } else { _waveOutEvent.Stop(); _mainWindow.PlayTrackBtn.Content = "Stopped"; _isPlayingNow = false; } }
public override async Task <TrackFile> GetDownloadableTrackAsync(Track track) { // Only property we need to set is Track.StoreId (see Google.Music/GoogleMusicApi.UWP/Requests/Data/StreamUrlGetRequest.cs:32) var streamUrl = await client.GetStreamUrlAsync(new GoogleMusicApi.Structure.Track { StoreId = track.Id }); if (streamUrl == null) { throw new InvalidSessionException("Play Music: Stream URL unavailable. Check your subscription is active then try again."); } // Unfortunately I have forgotten the various stream qualities available on Play Music because my subscription ran out, // so I will set the bitrate to -1, i.e. unknown // What is known is that all streams are MP3, so this should work. return(new TrackFile { BitRate = -1, DownloadUri = streamUrl, FileType = MediaFileTypes.Mpeg3Audio, Track = track }); }
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"); } }