private async void UpdateMusicSource() { //Set the new music source switch (settings.MusicSource) { //None case Settings.MusicSources.None: //Dispose the Spotify stuff try { spotifyTimer.Dispose(); spotifyAPI.Disconnect(); } catch {} //Dispose the Youtube stuff try { youtubeTimer.Dispose(); } catch {} //Update music source image Dispatcher.Invoke(() => musicSourceImage.Source = null); //Clear the display newSong = new Song("", "", ""); UpdateSongDisplay(); break; //Youtube Music case Settings.MusicSources.Youtube: //Dispose the Spotify stuff try { spotifyTimer.Dispose(); spotifyAPI.Disconnect(); } catch { } youtubeTimer = new Timer(UpdateYoutubeSong, new AutoResetEvent(false), 0, 2000); //Update music source image Dispatcher.Invoke(() => musicSourceImage.Source = new BitmapImage(new Uri("http://mrhumagames.com/MrhumasMusicOverlay/Youtube.png", UriKind.Absolute))); break; //Spotify case Settings.MusicSources.Spotify: //Authenticate the Spotify program await spotifyAPI.Authenticate(); //If authorization failed if (!spotifyAPI.authorized) { //Set the source to none settings.MusicSource = Settings.MusicSources.None; UpdateMusicSource(); return; } //Dispose the Youtube stuff try { youtubeTimer.Dispose(); } catch {} //Start timer to update display spotifyTimer = new Timer(UpdateSpotifySong, new AutoResetEvent(false), 0, 2000); //Update music source image Dispatcher.Invoke(() => musicSourceImage.Source = new BitmapImage(new Uri("http://mrhumagames.com/MrhumasMusicOverlay/Spotify.png", UriKind.Absolute))); break; } }
//Checks if a new song is playing or if the music was paused private void UpdateSongDisplay() { if (currentSong == null) //If no song has been played yet during this session { //Reset the timers songTimer.Change(dueTime, Period); artistTimer.Change(dueTime, Period); //Reset the indexes songTextIndex = 0; artistTextIndex = 0; //Update the displays Dispatcher.Invoke(() => { if (newSong.albumArt != "" && newSong.albumArt != null) { //Updates the album art on the display albumArtImage.Source = new BitmapImage(new Uri(newSong.albumArt)); } //Update the title and author text songNameText.Text = newSong.Title; artistNameText.Text = newSong.Artist; }); //Update the text length songTextLength = newSong.Title.Length; artistTextLength = newSong.Artist.Length; //Update the song variable currentSong = new Song(newSong.Title, newSong.Artist, newSong.albumArt); } //If a new song is playing, or a song gets resumed else if (newSong.Title != currentSong.Title || newSong.Artist != currentSong.Artist) { //Reset the timers songTimer.Change(dueTime, Period); artistTimer.Change(dueTime, Period); //Reset the indexes songTextIndex = 0; artistTextIndex = 0; //Update display Dispatcher.Invoke(() => { if (newSong.albumArt != "" && newSong.albumArt != null) { //Updates the album art on the display albumArtImage.Source = new BitmapImage(new Uri(newSong.albumArt)); } else { albumArtImage.Source = null; } //Update the title and author text songNameText.Text = newSong.Title; artistNameText.Text = newSong.Artist; //Set the title and author text to the beginning songNameText.ScrollToHome(); artistNameText.ScrollToHome(); }); //Update the text length songTextLength = newSong.Title.Length; artistTextLength = newSong.Artist.Length; //Update the song variable currentSong = new Song(newSong.Title, newSong.Artist, newSong.albumArt); } }