Made some LocalAPI methods async - Updated example, now without SynchronizingObject

This commit is contained in:
Jonas Dellinger 2016-09-09 14:56:37 +02:00
parent 3c82b0376b
commit ec8b7a3d3d
2 changed files with 38 additions and 16 deletions

View File

@ -4,6 +4,7 @@ using SpotifyAPI.Local.Models;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SpotifyAPI.Example
@ -22,7 +23,7 @@ namespace SpotifyAPI.Example
_spotify.OnTrackChange += _spotify_OnTrackChange;
_spotify.OnTrackTimeChange += _spotify_OnTrackTimeChange;
_spotify.OnVolumeChange += _spotify_OnVolumeChange;
_spotify.SynchronizingObject = this;
//_spotify.SynchronizingObject = this;
artistLinkLabel.Click += (sender, args) => Process.Start(artistLinkLabel.Tag.ToString());
albumLinkLabel.Click += (sender, args) => Process.Start(albumLinkLabel.Tag.ToString());
@ -108,22 +109,42 @@ namespace SpotifyAPI.Example
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)
{
timeLabel.Text = $"{FormatTime(e.TrackTime)}/{FormatTime(_currentTrack.Length)}";
if (InvokeRequired)
{
Invoke(new Action(() => _spotify_OnTrackTimeChange(sender, e)));
return;
}
timeLabel.Text = $@"{FormatTime(e.TrackTime)}/{FormatTime(_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);
}
@ -132,19 +153,19 @@ namespace SpotifyAPI.Example
Connect();
}
private void playUrlBtn_Click(object sender, EventArgs e)
private async void playUrlBtn_Click(object sender, EventArgs e)
{
_spotify.PlayURL(playTextBox.Text, contextTextBox.Text);
await _spotify.PlayURL(playTextBox.Text, contextTextBox.Text);
}
private void playBtn_Click(object sender, EventArgs e)
private async void playBtn_Click(object sender, EventArgs e)
{
_spotify.Play();
await _spotify.Play();
}
private void pauseBtn_Click(object sender, EventArgs e)
private async void pauseBtn_Click(object sender, EventArgs e)
{
_spotify.Pause();
await _spotify.Pause();
}
private void prevBtn_Click(object sender, EventArgs e)

View File

@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Timers;
namespace SpotifyAPI.Local
@ -234,17 +235,17 @@ namespace SpotifyAPI.Local
/// <summary>
/// Pause function
/// </summary>
public void Pause()
public async Task Pause()
{
_rh.SendPauseRequest();
await _rh.SendPauseRequest();
}
/// <summary>
/// Play function
/// </summary>
public void Play()
public async Task Play()
{
_rh.SendPlayRequest();
await _rh.SendPlayRequest();
}
/// <summary>
@ -265,9 +266,9 @@ namespace SpotifyAPI.Local
/// <remarks>
/// 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/
/// </remarks>
public void PlayURL(string uri, string context = "")
public async Task PlayURL(string uri, string context = "")
{
_rh.SendPlayRequest(uri, context);
await _rh.SendPlayRequest(uri, context);
}
/// <summary>
@ -275,9 +276,9 @@ namespace SpotifyAPI.Local
/// </summary>
/// <param name="uri">The Spotify URI</param>
[Obsolete("This method doesn't work with the current spotify version.")]
public void AddToQueue(string uri)
public async Task AddToQueue(string uri)
{
_rh.SendQueueRequest(uri);
await _rh.SendQueueRequest(uri);
}
/// <summary>