public static void PlayPause() { SpotifyLocalAPI _spotify = new SpotifyLocalAPI(); if (!_spotify.Connect()) { return; } StatusResponse status = _spotify.GetStatus(); if (status == null) { return; } if (status.Playing) { _spotify.Pause(); } else { _spotify.Play(); } _spotify = null; }
void work() { uint refreshrate = (uint)spinRefreshRate.Value; string tmp, currentsong = string.Empty; switch (currentWork) { case WorkType.VLC: #region VLC string hostname = txtVlcHostname.Text; uint port = (uint)spinVlcPort.Value; string password = txtVlcPassword.Text; VlcController conn; try { conn = new VlcController(hostname, port); conn.Authenticate(password); if (!conn.Authenticated) { Application.Invoke(delegate{ SetStatus("Wrong password, please retry!");}); stop(); } Application.Invoke(delegate{ SetStatus("VLC worker running");}); while (true) { tmp = conn.GetCurrentSongTitle(); if(tmp!= currentsong) { currentsong = tmp; File.WriteAllText(filepath, tmp); } Thread.Sleep ((int)refreshrate); } } catch(SocketException ex) { Application.Invoke(delegate{ SetStatus("Can't connect to the VLC server / Connection aborted by the server");}); stop(); } break; #endregion case WorkType.Spotify: #region Spotify spotify = new SpotifyLocalAPI(); if (!SpotifyLocalAPI.IsSpotifyRunning()) { Application.Invoke(delegate{ SetStatus("Spotify isn't running!");}); stop(); } else if (!SpotifyLocalAPI.IsSpotifyWebHelperRunning()) { Application.Invoke(delegate{ SetStatus("SpotifyWebHelper isn't running!");}); stop(); } else if (!spotify.Connect()) { Application.Invoke(delegate{ SetStatus("Can't connect to the SpotifyWebHelper.");}); stop(); } Application.Invoke(delegate{ SetStatus("Spotify worker running");}); while (true) { StatusResponse status = spotify.GetStatus(); if(status != null && status.Track != null) { tmp = status.Track.TrackResource.Uri; if(tmp != currentsong) { string format, formatted; currentsong = tmp; format = txtSpotifyFormat.Text; format = format.Replace("%t", FormatterCodes.Title); format = format.Replace("%a", FormatterCodes.Artist); format = format.Replace("%A", FormatterCodes.Album); formatted = format.Replace(FormatterCodes.Title, status.Track.TrackResource.Name); formatted = formatted.Replace(FormatterCodes.Artist, status.Track.ArtistResource.Name); formatted = formatted.Replace(FormatterCodes.Album, status.Track.AlbumResource.Name); File.WriteAllText(filepath, formatted); } } Thread.Sleep ((int)refreshrate); } #endregion default: Application.Invoke (delegate {SetStatus ("Error: Invalid work selected");}); stop (); break; } }
public void Start() { if (!api.IsSpotifyRunning()) { DialogResult q = MessageBox.Show("Spotify is not running! Would you like to start it now?", "Spotify not running!", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (q == DialogResult.Yes) { api.RunSpotify(); if (!api.IsSpotifyWebHelperRunning()) api.RunSpotifyWebHelper(); } else if (q == DialogResult.No) { Environment.Exit(0); } } _spotify = new api(); _spotify.Connect(); _spotify.OnTrackChange += new api.TrackChangeEventHandler(onTrackChange); _spotify.ListenForEvents = true; _currentTrack = _spotify.GetStatus().Track; WriteWithColor("+---------------------------+\n| ######################### |\n| ###### Spotify? ######### |\n| ######### Shut up! ###### |\n| ######################### |\n+---------------------------+", ConsoleColor.Gray); WriteWithColor(" Created by iUltimateLP @ GitHub [Compiled 10/28/15]", ConsoleColor.Gray); WriteWithColor("\nType 'help' for help.", ConsoleColor.Gray); checkAdStateTimer = new System.Timers.Timer(); checkAdStateTimer.Interval = 500; checkAdStateTimer.Elapsed += new System.Timers.ElapsedEventHandler(CheckAdStateTimer_Elapsed); checkAdStateTimer.Enabled = true; KeepConsoleRunning(); }
private async void ClientListener(string clientKey, CancellationToken token) { var spotify = new SpotifyLocalAPI(); var redisClient = new RedisClient(clientKey); if (!TryConnect(spotify)) { return; } while (true) { if (token.IsCancellationRequested) { redisClient.Delete($"Connected:{CurrentInstance}"); return; } var status = spotify.GetStatus(); var track = redisClient.Get<string>("TrackChange"); if (status.Track.TrackResource.Uri != track) { spotify.PlayURL(track); } var state = redisClient.GetBoolean("PlayStateChange"); if (status.Playing != state) { if (state != null && state == true) { spotify.Play(); } else if (state != null) { spotify.Pause(); } } UpdateUsers(redisClient); await Task.Delay(WaitMillisecondsDelay); } }