forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalControl.cs
More file actions
251 lines (209 loc) · 8.56 KB
/
LocalControl.cs
File metadata and controls
251 lines (209 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using SpotifyAPI.Local;
using SpotifyAPI.Local.Enums;
using SpotifyAPI.Local.Models;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Forms;
namespace SpotifyAPI.Example
{
public partial class LocalControl : UserControl
{
private readonly SpotifyLocalAPIConfig _config;
private SpotifyLocalAPI _spotify;
private Track _currentTrack;
public LocalControl()
{
InitializeComponent();
_config = new SpotifyLocalAPIConfig
{
ProxyConfig = new ProxyConfig()
};
_spotify = new SpotifyLocalAPI(_config);
_spotify.OnPlayStateChange += _spotify_OnPlayStateChange;
_spotify.OnTrackChange += _spotify_OnTrackChange;
_spotify.OnTrackTimeChange += _spotify_OnTrackTimeChange;
_spotify.OnVolumeChange += _spotify_OnVolumeChange;
//_spotify.SynchronizingObject = this;
artistLinkLabel.Click += (sender, args) => Process.Start(artistLinkLabel.Tag.ToString());
albumLinkLabel.Click += (sender, args) => Process.Start(albumLinkLabel.Tag.ToString());
titleLinkLabel.Click += (sender, args) => Process.Start(titleLinkLabel.Tag.ToString());
}
public void Connect()
{
if (!SpotifyLocalAPI.IsSpotifyRunning())
{
MessageBox.Show(@"Spotify isn't running!");
return;
}
if (!SpotifyLocalAPI.IsSpotifyWebHelperRunning())
{
MessageBox.Show(@"SpotifyWebHelper isn't running!");
return;
}
bool successful = _spotify.Connect();
if (successful)
{
connectBtn.Text = @"Connection to Spotify successful";
connectBtn.Enabled = false;
UpdateInfos();
_spotify.ListenForEvents = true;
}
else
{
DialogResult res = MessageBox.Show(@"Couldn't connect to the spotify client. Retry?", @"Spotify", MessageBoxButtons.YesNo);
if (res == DialogResult.Yes)
Connect();
}
}
public void UpdateInfos()
{
StatusResponse status = _spotify.GetStatus();
if (status == null)
return;
//Basic Spotify Infos
UpdatePlayingStatus(status.Playing);
clientVersionLabel.Text = status.ClientVersion;
versionLabel.Text = status.Version.ToString();
repeatShuffleLabel.Text = status.Repeat + @" and " + status.Shuffle;
if (status.Track != null) //Update track infos
UpdateTrack(status.Track);
RefreshVolumeMixerVolume();
}
public async void UpdateTrack(Track track)
{
_currentTrack = track;
advertLabel.Text = track.IsAd() ? "ADVERT" : "";
timeProgressBar.Maximum = track.Length;
if (track.IsAd())
return; //Don't process further, maybe null values
titleLinkLabel.Text = track.TrackResource?.Name;
titleLinkLabel.Tag = track.TrackResource?.Uri;
artistLinkLabel.Text = track.ArtistResource?.Name;
artistLinkLabel.Tag = track.ArtistResource?.Uri;
albumLinkLabel.Text = track.AlbumResource?.Name;
albumLinkLabel.Tag = track.AlbumResource?.Uri;
SpotifyUri uri = track.TrackResource?.ParseUri();
trackInfoBox.Text = $@"Track Info - {uri?.Id}";
bigAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size640, _config.ProxyConfig) : null;
smallAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size160, _config.ProxyConfig) : null;
}
public void UpdatePlayingStatus(bool playing)
{
isPlayingLabel.Text = playing.ToString();
}
public void RefreshVolumeMixerVolume()
{
volumeMixerLabel.Text = _spotify.GetSpotifyVolume().ToString(CultureInfo.InvariantCulture);
}
private void applyProxyBtn_Click(object sender, EventArgs e)
{
_config.ProxyConfig.Host = proxyHostTextBox.Text;
_config.ProxyConfig.Port = (int)proxyPortUpDown.Value;
_config.ProxyConfig.Username = proxyUsernameTextBox.Text;
_config.ProxyConfig.Password = proxyPasswordTextBox.Text;
bool connected = _spotify.ListenForEvents;
if (connected)
{
// Reconnect using new proxy
_spotify.ListenForEvents = false;
_spotify.OnPlayStateChange -= _spotify_OnPlayStateChange;
_spotify.OnTrackChange -= _spotify_OnTrackChange;
_spotify.OnTrackTimeChange -= _spotify_OnTrackTimeChange;
_spotify.OnVolumeChange -= _spotify_OnVolumeChange;
_spotify.Dispose();
_spotify = new SpotifyLocalAPI(_config);
_spotify.OnPlayStateChange += _spotify_OnPlayStateChange;
_spotify.OnTrackChange += _spotify_OnTrackChange;
_spotify.OnTrackTimeChange += _spotify_OnTrackTimeChange;
_spotify.OnVolumeChange += _spotify_OnVolumeChange;
connectBtn.Text = @"Reconnecting...";
Connect();
}
}
private void _spotify_OnVolumeChange(object sender, VolumeChangeEventArgs e)
{
if (InvokeRequired)
{
Invoke(new Action(() => _spotify_OnVolumeChange(sender, e)));
return;
}
volumeLabel.Text = (e.NewVolume * 100).ToString(CultureInfo.InvariantCulture);
}
private void _spotify_OnTrackTimeChange(object sender, TrackTimeChangeEventArgs e)
{
if (InvokeRequired)
{
Invoke(new Action(() => _spotify_OnTrackTimeChange(sender, e)));
return;
}
timeLabel.Text = $@"{FormatTime(e.TrackTime)}/{FormatTime(_currentTrack.Length)}";
if(e.TrackTime < _currentTrack.Length)
timeProgressBar.Value = (int)e.TrackTime;
}
private void _spotify_OnTrackChange(object sender, TrackChangeEventArgs e)
{
if (InvokeRequired)
{
Invoke(new Action(() => _spotify_OnTrackChange(sender, e)));
return;
}
UpdateTrack(e.NewTrack);
}
private void _spotify_OnPlayStateChange(object sender, PlayStateEventArgs e)
{
if (InvokeRequired)
{
Invoke(new Action(() => _spotify_OnPlayStateChange(sender, e)));
return;
}
UpdatePlayingStatus(e.Playing);
}
private void connectBtn_Click(object sender, EventArgs e)
{
Connect();
}
private async void playUrlBtn_Click(object sender, EventArgs e)
{
await _spotify.PlayURL(playTextBox.Text, contextTextBox.Text);
}
private async void playBtn_Click(object sender, EventArgs e)
{
await _spotify.Play();
}
private async void pauseBtn_Click(object sender, EventArgs e)
{
await _spotify.Pause();
}
private void prevBtn_Click(object sender, EventArgs e)
{
_spotify.Previous();
}
private void skipBtn_Click(object sender, EventArgs e)
{
_spotify.Skip();
}
private void volumeUpBtn_Click(object sender, EventArgs e)
{
float currentVolume = _spotify.GetSpotifyVolume();
float newVolume = currentVolume + 2.0f;
_spotify.SetSpotifyVolume(newVolume >= 100.0f ? 100.0f : newVolume);
RefreshVolumeMixerVolume();
}
private void volumeDownBtn_Click(object sender, EventArgs e)
{
float currentVolume = _spotify.GetSpotifyVolume();
float newVolume = currentVolume - 2.0f;
_spotify.SetSpotifyVolume(newVolume <= 0.0f ? 0.0f : newVolume);
RefreshVolumeMixerVolume();
}
private static String FormatTime(double sec)
{
TimeSpan span = TimeSpan.FromSeconds(sec);
String secs = span.Seconds.ToString(), mins = span.Minutes.ToString();
if (secs.Length < 2)
secs = "0" + secs;
return mins + ":" + secs;
}
}
}