Spotify.NET/SpotifyAPI/SpoitfyLocalAPI/SpotifyMusicHandler.cs

155 lines
4.6 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;
2014-01-10 07:09:14 +00:00
using System.Runtime.InteropServices;
using System.Windows.Forms;
2014-02-01 12:52:33 +00:00
using System.IO;
2014-01-07 15:26:03 +00:00
namespace SpotifyAPI.SpotifyLocalAPI
2014-01-07 15:26:03 +00:00
{
2014-02-01 12:52:33 +00:00
public class SpotifyMusicHandler
2014-01-07 15:26:03 +00:00
{
2014-01-10 07:09:14 +00:00
[DllImport("user32.dll")]
2014-02-13 13:23:52 +00:00
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
2014-02-01 12:52:33 +00:00
[DllImport("nircmd.dll")]
2014-02-13 13:23:52 +00:00
private static extern bool DoNirCmd(String NirCmdStr);
2014-01-10 07:09:14 +00:00
2014-01-08 22:22:54 +00:00
RemoteHandler rh;
StatusResponse sr;
2014-02-13 13:23:52 +00:00
//Constants for the Keyboard Event (NextTrack + PreviousTrack)
2014-01-10 07:09:14 +00:00
const byte VK_MEDIA_NEXT_TRACK = 0xb0;
const byte VK_MEDIA_PREV_TRACK = 0xb1;
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
2014-01-10 07:09:14 +00:00
2014-02-01 12:52:33 +00:00
public SpotifyMusicHandler()
2014-01-07 15:26:03 +00:00
{
2014-01-08 22:22:54 +00:00
rh = RemoteHandler.GetInstance();
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Simulates a KeyPress
/// </summary>
/// <param name="keyCode">The keycode for the represented Key</param>
2014-01-10 07:09:14 +00:00
void PressKey(byte keyCode)
{
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Checks if a song is playing
/// </summary>
/// <returns>True if a song is playing, false if not</returns>
2014-01-08 22:22:54 +00:00
public Boolean IsPlaying()
{
return sr.playing;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Returns the current Volume
/// </summary>
/// <returns>A value between 0 and 1</returns>
2014-02-01 12:52:33 +00:00
public double GetVolume()
{
return sr.volume;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Plays a Spotify URI
/// </summary>
/// <param name="uri">The Spotify URI. Can be checked with <seealso cref="SpotifyLocalAPIClass.IsValidSpotifyURI"/></param>
2014-02-13 13:23:52 +00:00
public void PlayURL(String uri)
2014-02-01 12:52:33 +00:00
{
2014-02-13 13:23:52 +00:00
rh.SendPlayRequest(uri);
2014-02-01 12:52:33 +00:00
}
2014-02-13 13:23:52 +00:00
/// <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>
2014-02-13 13:23:52 +00:00
/// Checks if the current "Track" is an Advert
/// </summary>
/// <returns>True if it's an Advert, false if not</returns>
2014-02-01 12:52:33 +00:00
public Boolean IsAdRunning()
{
return !sr.next_enabled && !sr.prev_enabled;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Returns the current Track object
/// </summary>
/// <returns>Returns the current track object</returns>
2014-01-08 22:22:54 +00:00
public Track GetCurrentTrack()
{
return sr.track;
2014-01-07 15:26:03 +00:00
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Skips the current song (Using keypress simulation)
/// </summary>
2014-01-10 07:09:14 +00:00
public void Skip()
{
PressKey(VK_MEDIA_NEXT_TRACK);
}
2014-02-13 13:23:52 +00:00
/// <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>
2014-01-10 07:09:14 +00:00
public double GetTrackPosition()
{
return sr.playing_position;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Mutes Spotify (Requires nircmd.dll)
/// </summary>
2014-02-01 12:52:33 +00:00
public void Mute()
{
if(File.Exists("nircmd.dll"))
DoNirCmd("muteappvolume spotify.exe 1");
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Unmutes Spotify (Requires nircmd.dll)
/// </summary>
2014-02-01 12:52:33 +00:00
public void UnMute()
{
if (File.Exists("nircmd.dll"))
DoNirCmd("muteappvolume spotify.exe 0");
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Pause function
/// </summary>
2014-01-10 07:09:14 +00:00
public void Pause()
{
rh.SendPauseRequest();
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Play function
/// </summary>
2014-01-10 07:09:14 +00:00
public void Play()
{
rh.SendPlayRequest();
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Returns all Informations gathered by the JSON Request
/// </summary>
/// <returns>A StatusResponse object</returns>
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-02-13 13:23:52 +00:00
//Used internaly
2014-01-08 22:22:54 +00:00
internal void Update(StatusResponse sr)
{
this.sr = sr;
2014-01-07 15:26:03 +00:00
}
}
}