Repository navigation
Expand file tree
/
Copy pathSpotifyMusicHandler.cs
More file actions
154 lines (150 loc) · 4.6 KB
/
Copy pathSpotifyMusicHandler.cs
File metadata and controls
154 lines (150 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
namespace SpotifyAPI.SpotifyLocalAPI
{
public class SpotifyMusicHandler
{
[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
[DllImport("nircmd.dll")]
private static extern bool DoNirCmd(String NirCmdStr);
RemoteHandler rh;
StatusResponse sr;
//Constants for the Keyboard Event (NextTrack + PreviousTrack)
const byte VK_MEDIA_NEXT_TRACK = 0xb0;
const byte VK_MEDIA_PREV_TRACK = 0xb1;
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
public SpotifyMusicHandler()
{
rh = RemoteHandler.GetInstance();
}
/// <summary>
/// Simulates a KeyPress
/// </summary>
/// <param name="keyCode">The keycode for the represented Key</param>
void PressKey(byte keyCode)
{
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
/// <summary>
/// Checks if a song is playing
/// </summary>
/// <returns>True if a song is playing, false if not</returns>
public Boolean IsPlaying()
{
return sr.playing;
}
/// <summary>
/// Returns the current Volume
/// </summary>
/// <returns>A value between 0 and 1</returns>
public double GetVolume()
{
return sr.volume;
}
/// <summary>
/// Plays a Spotify URI
/// </summary>
/// <param name="uri">The Spotify URI. Can be checked with <seealso cref="SpotifyLocalAPIClass.IsValidSpotifyURI"/></param>
public void PlayURL(String uri)
{
rh.SendPlayRequest(uri);
}
/// <summary>
/// Adds a Spotify URI to the Queue
/// </summary>
/// <param name="uri">The Spotify URI. Can be checked with <seealso cref="SpotifyLocalAPIClass.IsValidSpotifyURI"/></param>
public void AddToQueue(String uri)
{
rh.SendQueueRequest(uri);
}
/// <summary>
/// Checks if the current "Track" is an Advert
/// </summary>
/// <returns>True if it's an Advert, false if not</returns>
public Boolean IsAdRunning()
{
return !sr.next_enabled && !sr.prev_enabled;
}
/// <summary>
/// Returns the current Track object
/// </summary>
/// <returns>Returns the current track object</returns>
public Track GetCurrentTrack()
{
return sr.track;
}
/// <summary>
/// Skips the current song (Using keypress simulation)
/// </summary>
public void Skip()
{
PressKey(VK_MEDIA_NEXT_TRACK);
}
/// <summary>
/// Emulates the "Previous" Key (Using keypress simulation)
/// </summary>
public void Previous()
{
PressKey(VK_MEDIA_PREV_TRACK);
}
/// <summary>
/// Returns the current track postion
/// </summary>
/// <returns>A double between 0 and ∞</returns>
public double GetTrackPosition()
{
return sr.playing_position;
}
/// <summary>
/// Mutes Spotify (Requires nircmd.dll)
/// </summary>
public void Mute()
{
if(File.Exists("nircmd.dll"))
DoNirCmd("muteappvolume spotify.exe 1");
}
/// <summary>
/// Unmutes Spotify (Requires nircmd.dll)
/// </summary>
public void UnMute()
{
if (File.Exists("nircmd.dll"))
DoNirCmd("muteappvolume spotify.exe 0");
}
/// <summary>
/// Pause function
/// </summary>
public void Pause()
{
rh.SendPauseRequest();
}
/// <summary>
/// Play function
/// </summary>
public void Play()
{
rh.SendPlayRequest();
}
/// <summary>
/// Returns all Informations gathered by the JSON Request
/// </summary>
/// <returns>A StatusResponse object</returns>
public StatusResponse GetStatusResponse()
{
return sr;
}
//Used internaly
internal void Update(StatusResponse sr)
{
this.sr = sr;
}
}
}