Skip to content

Commit ec8b7a3

Browse files
author
Jonas Dellinger
committed
Made some LocalAPI methods async - Updated example, now without SynchronizingObject
1 parent 3c82b03 commit ec8b7a3

2 files changed

Lines changed: 38 additions & 16 deletions

File tree

SpotifyAPI.Example/LocalControl.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Diagnostics;
66
using System.Globalization;
7+
using System.Threading.Tasks;
78
using System.Windows.Forms;
89

910
namespace SpotifyAPI.Example
@@ -22,7 +23,7 @@ public LocalControl()
2223
_spotify.OnTrackChange += _spotify_OnTrackChange;
2324
_spotify.OnTrackTimeChange += _spotify_OnTrackTimeChange;
2425
_spotify.OnVolumeChange += _spotify_OnVolumeChange;
25-
_spotify.SynchronizingObject = this;
26+
//_spotify.SynchronizingObject = this;
2627

2728
artistLinkLabel.Click += (sender, args) => Process.Start(artistLinkLabel.Tag.ToString());
2829
albumLinkLabel.Click += (sender, args) => Process.Start(albumLinkLabel.Tag.ToString());
@@ -108,22 +109,42 @@ public void UpdatePlayingStatus(bool playing)
108109

109110
private void _spotify_OnVolumeChange(object sender, VolumeChangeEventArgs e)
110111
{
112+
if (InvokeRequired)
113+
{
114+
Invoke(new Action(() => _spotify_OnVolumeChange(sender, e)));
115+
return;
116+
}
111117
volumeLabel.Text = (e.NewVolume * 100).ToString(CultureInfo.InvariantCulture);
112118
}
113119

114120
private void _spotify_OnTrackTimeChange(object sender, TrackTimeChangeEventArgs e)
115121
{
116-
timeLabel.Text = $"{FormatTime(e.TrackTime)}/{FormatTime(_currentTrack.Length)}";
122+
if (InvokeRequired)
123+
{
124+
Invoke(new Action(() => _spotify_OnTrackTimeChange(sender, e)));
125+
return;
126+
}
127+
timeLabel.Text = $@"{FormatTime(e.TrackTime)}/{FormatTime(_currentTrack.Length)}";
117128
timeProgressBar.Value = (int)e.TrackTime;
118129
}
119130

120131
private void _spotify_OnTrackChange(object sender, TrackChangeEventArgs e)
121132
{
133+
if (InvokeRequired)
134+
{
135+
Invoke(new Action(() => _spotify_OnTrackChange(sender, e)));
136+
return;
137+
}
122138
UpdateTrack(e.NewTrack);
123139
}
124140

125141
private void _spotify_OnPlayStateChange(object sender, PlayStateEventArgs e)
126142
{
143+
if (InvokeRequired)
144+
{
145+
Invoke(new Action(() => _spotify_OnPlayStateChange(sender, e)));
146+
return;
147+
}
127148
UpdatePlayingStatus(e.Playing);
128149
}
129150

@@ -132,19 +153,19 @@ private void connectBtn_Click(object sender, EventArgs e)
132153
Connect();
133154
}
134155

135-
private void playUrlBtn_Click(object sender, EventArgs e)
156+
private async void playUrlBtn_Click(object sender, EventArgs e)
136157
{
137-
_spotify.PlayURL(playTextBox.Text, contextTextBox.Text);
158+
await _spotify.PlayURL(playTextBox.Text, contextTextBox.Text);
138159
}
139160

140-
private void playBtn_Click(object sender, EventArgs e)
161+
private async void playBtn_Click(object sender, EventArgs e)
141162
{
142-
_spotify.Play();
163+
await _spotify.Play();
143164
}
144165

145-
private void pauseBtn_Click(object sender, EventArgs e)
166+
private async void pauseBtn_Click(object sender, EventArgs e)
146167
{
147-
_spotify.Pause();
168+
await _spotify.Pause();
148169
}
149170

150171
private void prevBtn_Click(object sender, EventArgs e)

SpotifyAPI/Local/SpotifyLocalAPI.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.IO;
66
using System.Runtime.InteropServices;
7+
using System.Threading.Tasks;
78
using System.Timers;
89

910
namespace SpotifyAPI.Local
@@ -234,17 +235,17 @@ public float GetSpotifyVolume()
234235
/// <summary>
235236
/// Pause function
236237
/// </summary>
237-
public void Pause()
238+
public async Task Pause()
238239
{
239-
_rh.SendPauseRequest();
240+
await _rh.SendPauseRequest();
240241
}
241242

242243
/// <summary>
243244
/// Play function
244245
/// </summary>
245-
public void Play()
246+
public async Task Play()
246247
{
247-
_rh.SendPlayRequest();
248+
await _rh.SendPlayRequest();
248249
}
249250

250251
/// <summary>
@@ -265,19 +266,19 @@ internal void PressKey(byte keyCode)
265266
/// <remarks>
266267
/// Contexts are basically a queue in spotify. a song can be played within a context, meaning that hitting next / previous would lead to another song. Contexts are leveraged by widgets as described in the "Multiple tracks player" section of the following documentation page: https://developer.spotify.com/technologies/widgets/spotify-play-button/
267268
/// </remarks>
268-
public void PlayURL(string uri, string context = "")
269+
public async Task PlayURL(string uri, string context = "")
269270
{
270-
_rh.SendPlayRequest(uri, context);
271+
await _rh.SendPlayRequest(uri, context);
271272
}
272273

273274
/// <summary>
274275
/// Adds a Spotify URI to the Queue
275276
/// </summary>
276277
/// <param name="uri">The Spotify URI</param>
277278
[Obsolete("This method doesn't work with the current spotify version.")]
278-
public void AddToQueue(string uri)
279+
public async Task AddToQueue(string uri)
279280
{
280-
_rh.SendQueueRequest(uri);
281+
await _rh.SendQueueRequest(uri);
281282
}
282283

283284
/// <summary>

0 commit comments

Comments
 (0)