Spotify.NET/SpotifyAPI/SpotifyEventHandler.cs

112 lines
3.8 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;
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;
2014-02-13 13:23:52 +00:00
timer.Enabled = false;
2014-01-08 22:22:54 +00:00
this.api = api;
this.mh = mh;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// If Events should be triggered
/// </summary>
/// <param name="listen">True if you want to listen for events, false if not</param>
2014-01-08 22:22:54 +00:00
public void ListenForEvents(Boolean listen)
{
2014-02-13 13:23:52 +00:00
timer.Enabled = listen;
2014-01-08 22:22:54 +00:00
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Sets a synchronizing object, so you don't need to Invoke
/// </summary>
/// <param name="obj">The SynchronizingObject e.g a Form</param>
2014-02-01 12:52:33 +00:00
public void SetSynchronizingObject(System.ComponentModel.ISynchronizeInvoke obj)
{
timer.SynchronizingObject = obj;
}
2014-02-13 13:23:52 +00:00
internal void tick(object sender, EventArgs e)
2014-01-08 22:22:54 +00:00
{
api.Update();
if (response == null)
{
response = mh.GetStatusResponse();
timer.Start();
return;
}
StatusResponse new_response = mh.GetStatusResponse();
if(new_response == null)
{
timer.Start();
return;
}
2014-02-01 12:52:33 +00:00
if (!new_response.running && new_response.track == null)
{
timer.Start();
2014-02-01 12:52:33 +00:00
return;
}
2014-02-14 12:04:45 +00:00
if (new_response.track != null && response.track != null)
2014-01-08 22:22:54 +00:00
{
2014-02-14 12:04:45 +00:00
if (new_response.track.GetTrackName() != response.track.GetTrackName() && OnTrackChange != null)
2014-01-08 22:22:54 +00:00
{
2014-02-14 12:04:45 +00:00
OnTrackChange(new TrackChangeEventArgs()
{
old_track = response.track,
new_track = new_response.track
});
}
2014-01-08 22:22:54 +00:00
}
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();
}
}
}