|
1 | | -# WIP |
| 1 | +# Getting started |
| 2 | + |
| 3 | +This API provides some access to the local running Spotify-Client (Windows only). |
| 4 | +You can fetch details for the current track, play/pause, skip/previous track and |
| 5 | +get notified on various events. |
| 6 | + |
| 7 | +**NOTE:** This API is unofficial, things may brake in the future and there is no |
| 8 | +guarantee everything works out of the box. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## First steps |
| 13 | + |
| 14 | +**Imports** |
| 15 | +So after you added the API to your project, you may want to add following imports to your files: |
| 16 | + |
| 17 | +```cs |
| 18 | +using SpotifyAPI.Local; //Base Namespace |
| 19 | +using SpotifyAPI.Local.Enums; //Enums |
| 20 | +using SpotifyAPI.Local.Models; //Models for the JSON-responses |
| 21 | +using SpotifyAPI.Local; //Base Namespace |
| 22 | +``` |
| 23 | + |
| 24 | +**Basic-Usage** |
| 25 | +Now you can actually start fetching infos from your spotify client, just create a new Instance of SpotifyLocalAPI: |
| 26 | +```cs |
| 27 | +private static SpotifyLocalAPI _spotify; |
| 28 | + |
| 29 | +public static void Main(String[] args) |
| 30 | +{ |
| 31 | + _spotify = new SpotifyLocalAPI(); |
| 32 | + if (!SpotifyLocalAPI.IsSpotifyRunning()) |
| 33 | + return; //Make sure the spotify client is running |
| 34 | + if (!SpotifyLocalAPI.IsSpotifyWebHelperRunning()) |
| 35 | + return; //Make sure the WebHelper is running |
| 36 | +
|
| 37 | + if(!_spotify.Connect()) |
| 38 | + return; //We need to call Connect before fetching infos, this will handle Auth stuff |
| 39 | +
|
| 40 | + StatusResponse status = _spotify.GetStatus(); //status contains infos |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Anti-Virus Blocking Response |
| 45 | + |
| 46 | +Some Anti-Virus Software blocks the response from spotify due wrong headers. |
| 47 | +Currently, it's confirmed for AVG's LinkScanner and Bitdefender. |
| 48 | +Adding `http://SpotifyAPI.spotilocal.com:4380` to the URL-Exceptions seems to fix it for most users. |
| 49 | +More infos can be found [here](https://github.com/JohnnyCrazy/SpotifyAPI-NET/issues/51) |
| 50 | + |
| 51 | +## Client Status |
| 52 | + |
| 53 | +Calling `_spotify.GetStatus()` after connecting returns the following `StatusResponse`: |
| 54 | + |
| 55 | +``` |
| 56 | +public int Version { get; set; } |
| 57 | +
|
| 58 | +public string ClientVersion { get; set; } |
| 59 | +
|
| 60 | +public bool Playing { get; set; } |
| 61 | +
|
| 62 | +public bool Shuffle { get; set; } |
| 63 | +
|
| 64 | +public bool Repeat { get; set; } |
| 65 | +
|
| 66 | +public bool PlayEnabled { get; set; } |
| 67 | +
|
| 68 | +public bool PrevEnabled { get; set; } |
| 69 | +
|
| 70 | +public bool NextEnabled { get; set; } |
| 71 | +
|
| 72 | +public Track Track { get; set; } |
| 73 | +
|
| 74 | +public double PlayingPosition { get; set; } |
| 75 | +
|
| 76 | +public int ServerTime { get; set; } |
| 77 | +
|
| 78 | +public double Volume { get; set; } |
| 79 | +
|
| 80 | +public bool Online { get; set; } |
| 81 | +
|
| 82 | +public bool Running { get; set; } |
| 83 | +``` |
| 84 | + |
| 85 | +Most of the properties are self-explanatory, some notes: |
| 86 | + |
| 87 | +* `Shuffle` and `Repeat` currently always return `false` |
| 88 | + |
| 89 | +## Current Track |
| 90 | + |
| 91 | +The current Track can be fetched via `_spotify.GetStatus().Track` and contains following properties/methods: |
| 92 | + |
| 93 | +* `TrackResource` - `SpotifyResource` which contains Track `Name` and `Uri` |
| 94 | +* `AlbumResource` - `SpotifyResource` which contains Album `Name` and `Uri` |
| 95 | +* `ArtistResource` - `SpotifyResource` which contains Artist `Name` and `Uri` (Only the main artist will be listed) |
| 96 | +* `IsAd()` will check whether the current track is an AD |
| 97 | +* Various methods for getting the album art: |
| 98 | + * `string GetAlbumArtUrl(AlbumArtSize size)` |
| 99 | + * `Task<Bitmap> GetAlbumArtAsync(AlbumArtSize size)` |
| 100 | + * `Bitmap GetAlbumArt(AlbumArtSize size)` |
| 101 | + * `Task<byte[]> GetAlbumArtAsByteArrayAsync(AlbumArtSize size)` |
| 102 | + * `byte[] GetAlbumArtAsByteArray(AlbumArtSize size)` |
| 103 | + |
| 104 | +## Events |
| 105 | + |
| 106 | +To receive events, make sure you listen for them `_spotify.ListenForEvents = true;` |
| 107 | +You can set a `SynchronizingObject`, then the events will be called on the specific context |
| 108 | + |
| 109 | +Following events can be overriden: |
| 110 | + |
| 111 | +* `OnPlayStateChange` - triggers when the player changes from `play` to `pause` and vice versa |
| 112 | +* `OnTrackChange` - triggers when a new track will be played |
| 113 | +* `OnTrackTimeChange` - triggers when a track is playing and track-time changes |
| 114 | +* `OnVolumeChange` - triggeres when the internal volume of spotify changes |
| 115 | + |
| 116 | +## Methods |
| 117 | + |
| 118 | +Furthermore, following methods are available: |
| 119 | + |
| 120 | +* `void Mute()` - will mute the Spotify client via WindowsAPI |
| 121 | +* `void UnMute()` - will unmute the Spotify client via WindowsAPI |
| 122 | +* `bool IsSpotifyMuted()` - will return wether the Spotify client is muted |
| 123 | +* `void SetSpotifyVolume(float volume = 100)` - sets the windows volume of spotify (0 - 100) |
| 124 | +* `float GetSpotifyVolume()` - returns the windows volume of spotify (0 - 100) |
| 125 | +* `void Pause()` - will pause spotify's playback |
| 126 | +* `void Play()` - will resume spotify's playback |
| 127 | +* `void PlayURL(string uri, string context = "")` - will play a spotify URI (track/album/playlist) in the specifc context (can be a album/playlist URI) |
| 128 | +* `void Skip()` - will skip the track via an emulated media key |
| 129 | +* `void Previous()` - will play the previous track via an emulated media key |
| 130 | +* `bool IsSpotifyRunning()` - returns true if a spotify client instance is running, false if not |
| 131 | +* `bool IsSpotifyWebHelperRunning()` - returns true if a spotify web-helper instance is running, false if not |
| 132 | +* `void RunSpotify()` - will attempt to start a Spotify instance |
| 133 | +* `void RunSpotifyWebHelper()` - will attempt to start a Spotify web-helper instance |
0 commit comments