Spotify.NET/SpotifyAPI/SpotifyEventHandler.cs

101 lines
3.3 KiB
C#
Raw Normal View History

2014-01-08 22:22:54 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2014-02-01 12:52:33 +00:00
using SpotifyAPIv1;
2014-01-08 22:22:54 +00:00
namespace SpotifyAPIv1
{
2014-02-01 12:52:33 +00:00
public class SpotifyEventHandler
2014-01-08 22:22:54 +00:00
{
private Boolean listen = false;
private System.Timers.Timer timer;
private SpotifyAPI api;
2014-02-01 12:52:33 +00:00
private SpotifyMusicHandler mh;
2014-01-08 22:22:54 +00:00
private StatusResponse response;
2014-01-10 07:09:14 +00:00
public delegate void TrackChangeEventHandler(TrackChangeEventArgs e);
2014-01-08 22:22:54 +00:00
public delegate void PlayStateEventHandler(PlayStateEventArgs e);
public delegate void VolumeChangeEventHandler(VolumeChangeEventArgs e);
2014-01-10 07:09:14 +00:00
public delegate void TrackTimeChangeEventHandler(TrackTimeChangeEventArgs e);
public event TrackChangeEventHandler OnTrackChange;
2014-01-08 22:22:54 +00:00
public event PlayStateEventHandler OnPlayStateChange;
public event VolumeChangeEventHandler OnVolumeChange;
2014-01-10 07:09:14 +00:00
public event TrackTimeChangeEventHandler OnTrackTimeChange;
2014-01-08 22:22:54 +00:00
2014-02-01 12:52:33 +00:00
public SpotifyEventHandler(SpotifyAPI api, SpotifyMusicHandler mh)
2014-01-08 22:22:54 +00:00
{
timer = new System.Timers.Timer();
timer.Interval = 50;
timer.Elapsed += tick;
timer.AutoReset = false;
timer.Enabled = true;
timer.Start();
this.api = api;
this.mh = mh;
}
public void ListenForEvents(Boolean listen)
{
this.listen = listen;
}
2014-02-01 12:52:33 +00:00
public void SetSynchronizingObject(System.ComponentModel.ISynchronizeInvoke obj)
{
timer.SynchronizingObject = obj;
}
2014-01-10 07:09:14 +00:00
private void tick(object sender, EventArgs e)
2014-01-08 22:22:54 +00:00
{
2014-01-10 07:09:14 +00:00
if (!listen)
2014-01-08 22:22:54 +00:00
{
timer.Start();
return;
}
api.Update();
if (response == null)
{
response = mh.GetStatusResponse();
timer.Start();
return;
}
StatusResponse new_response = mh.GetStatusResponse();
2014-02-01 12:52:33 +00:00
if (!new_response.running && new_response.track == null)
return;
if (new_response.track.GetTrackName() != response.track.GetTrackName() && OnTrackChange != null)
2014-01-08 22:22:54 +00:00
{
2014-01-10 07:09:14 +00:00
OnTrackChange(new TrackChangeEventArgs()
2014-01-08 22:22:54 +00:00
{
old_track = response.track,
new_track = new_response.track
});
}
if (new_response.playing != response.playing && OnPlayStateChange != null)
{
OnPlayStateChange(new PlayStateEventArgs()
{
playing = new_response.playing
});
}
if (new_response.volume != response.volume && OnVolumeChange != null)
{
OnVolumeChange(new VolumeChangeEventArgs()
{
old_volume = response.volume,
new_volume = new_response.volume
});
}
2014-01-10 07:09:14 +00:00
if(new_response.playing_position != response.playing_position && OnTrackTimeChange != null)
{
OnTrackTimeChange(new TrackTimeChangeEventArgs()
{
track_time = new_response.playing_position
});
}
2014-01-08 22:22:54 +00:00
response = new_response;
timer.Start();
}
}
}