Spotify.NET/SpotifyAPI/MusicHandler.cs

73 lines
1.7 KiB
C#
Raw Normal View History

2014-01-07 15:26:03 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2014-01-10 07:09:14 +00:00
using System.Runtime.InteropServices;
using System.Windows.Forms;
2014-01-07 15:26:03 +00:00
namespace SpotifyAPIv1
{
2014-01-08 22:22:54 +00:00
public class MusicHandler
2014-01-07 15:26:03 +00:00
{
2014-01-10 07:09:14 +00:00
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
2014-01-08 22:22:54 +00:00
RemoteHandler rh;
StatusResponse sr;
2014-01-10 07:09:14 +00:00
const byte VK_MEDIA_NEXT_TRACK = 0xb0;
const byte VK_MEDIA_PREV_TRACK = 0xb1;
2014-01-07 15:26:03 +00:00
public MusicHandler()
{
2014-01-08 22:22:54 +00:00
rh = RemoteHandler.GetInstance();
}
2014-01-10 07:09:14 +00:00
void PressKey(byte keyCode)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
2014-01-08 22:22:54 +00:00
public Boolean IsPlaying()
{
return sr.playing;
}
2014-01-07 15:26:03 +00:00
2014-01-08 22:22:54 +00:00
public Track GetCurrentTrack()
{
return sr.track;
2014-01-07 15:26:03 +00:00
}
2014-01-10 07:09:14 +00:00
public void Skip()
{
PressKey(VK_MEDIA_NEXT_TRACK);
}
public double GetTrackPosition()
{
return sr.playing_position;
}
public void Previous()
{
PressKey(VK_MEDIA_PREV_TRACK);
}
public void Pause()
{
rh.SendPauseRequest();
}
public void Play()
{
rh.SendPlayRequest();
}
2014-01-08 22:22:54 +00:00
public StatusResponse GetStatusResponse()
2014-01-07 15:26:03 +00:00
{
2014-01-08 22:22:54 +00:00
return sr;
}
2014-01-07 15:26:03 +00:00
2014-01-08 22:22:54 +00:00
internal void Update(StatusResponse sr)
{
this.sr = sr;
2014-01-07 15:26:03 +00:00
}
}
}