using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; using System.IO; namespace SpotifyAPI.SpotifyLocalAPI { public class SpotifyMusicHandler { [DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); [DllImport("nircmd.dll")] private static extern bool DoNirCmd(String NirCmdStr); RemoteHandler rh; StatusResponse sr; //Constants for the Keyboard Event (NextTrack + PreviousTrack) const byte VK_MEDIA_NEXT_TRACK = 0xb0; const byte VK_MEDIA_PREV_TRACK = 0xb1; const int KEYEVENTF_EXTENDEDKEY = 0x1; const int KEYEVENTF_KEYUP = 0x2; public SpotifyMusicHandler() { rh = RemoteHandler.GetInstance(); } /// /// Simulates a KeyPress /// /// The keycode for the represented Key void PressKey(byte keyCode) { keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0); keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } /// /// Checks if a song is playing /// /// True if a song is playing, false if not public Boolean IsPlaying() { return sr.playing; } /// /// Returns the current Volume /// /// A value between 0 and 1 public double GetVolume() { return sr.volume; } /// /// Plays a Spotify URI /// /// The Spotify URI. Can be checked with public void PlayURL(String uri) { rh.SendPlayRequest(uri); } /// /// Adds a Spotify URI to the Queue /// /// The Spotify URI. Can be checked with public void AddToQueue(String uri) { rh.SendQueueRequest(uri); } /// /// Checks if the current "Track" is an Advert /// /// True if it's an Advert, false if not public Boolean IsAdRunning() { return !sr.next_enabled && !sr.prev_enabled; } /// /// Returns the current Track object /// /// Returns the current track object public Track GetCurrentTrack() { return sr.track; } /// /// Skips the current song (Using keypress simulation) /// public void Skip() { PressKey(VK_MEDIA_NEXT_TRACK); } /// /// Emulates the "Previous" Key (Using keypress simulation) /// public void Previous() { PressKey(VK_MEDIA_PREV_TRACK); } /// /// Returns the current track postion /// /// A double between 0 and ∞ public double GetTrackPosition() { return sr.playing_position; } /// /// Mutes Spotify (Requires nircmd.dll) /// public void Mute() { if(File.Exists("nircmd.dll")) DoNirCmd("muteappvolume spotify.exe 1"); } /// /// Unmutes Spotify (Requires nircmd.dll) /// public void UnMute() { if (File.Exists("nircmd.dll")) DoNirCmd("muteappvolume spotify.exe 0"); } /// /// Pause function /// public void Pause() { rh.SendPauseRequest(); } /// /// Play function /// public void Play() { rh.SendPlayRequest(); } /// /// Returns all Informations gathered by the JSON Request /// /// A StatusResponse object public StatusResponse GetStatusResponse() { return sr; } //Used internaly internal void Update(StatusResponse sr) { this.sr = sr; } } }