2014-02-01 12:52:33 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
2014-04-12 21:46:54 +01:00
|
|
|
|
using System.Diagnostics;
|
2014-02-01 12:52:33 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
2014-07-20 21:42:46 +01:00
|
|
|
|
using SpotifyAPI.SpotifyLocalAPI;
|
2014-02-17 11:58:15 +00:00
|
|
|
|
using System.Threading;
|
2014-07-20 21:42:46 +01:00
|
|
|
|
using SpotifyEventHandler = SpotifyAPI.SpotifyLocalAPI.SpotifyEventHandler;
|
2014-02-01 12:52:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace SpotifyAPI_Example
|
|
|
|
|
{
|
|
|
|
|
public partial class Form1 : Form
|
|
|
|
|
{
|
2014-07-20 21:42:46 +01:00
|
|
|
|
SpotifyLocalAPIClass spotify;
|
2014-02-01 12:52:33 +00:00
|
|
|
|
SpotifyMusicHandler mh;
|
|
|
|
|
SpotifyEventHandler eh;
|
|
|
|
|
public Form1()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2014-07-20 21:42:46 +01:00
|
|
|
|
spotify = new SpotifyLocalAPIClass();
|
|
|
|
|
if (!SpotifyLocalAPIClass.IsSpotifyRunning())
|
2014-02-17 11:58:15 +00:00
|
|
|
|
{
|
2014-02-01 12:52:33 +00:00
|
|
|
|
spotify.RunSpotify();
|
2014-02-17 12:03:00 +00:00
|
|
|
|
Thread.Sleep(5000);
|
2014-02-17 11:58:15 +00:00
|
|
|
|
}
|
2014-07-20 21:42:46 +01:00
|
|
|
|
|
|
|
|
|
if (!SpotifyLocalAPIClass.IsSpotifyWebHelperRunning())
|
2014-02-17 11:58:15 +00:00
|
|
|
|
{
|
2014-02-01 12:52:33 +00:00
|
|
|
|
spotify.RunSpotifyWebHelper();
|
2014-02-17 11:58:15 +00:00
|
|
|
|
Thread.Sleep(4000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!spotify.Connect())
|
|
|
|
|
{
|
|
|
|
|
Boolean retry = true;
|
|
|
|
|
while(retry)
|
|
|
|
|
{
|
2014-07-20 21:42:46 +01:00
|
|
|
|
if (MessageBox.Show("SpotifyLocalAPIClass could'nt load!", "Error", MessageBoxButtons.RetryCancel) == System.Windows.Forms.DialogResult.Retry)
|
2014-02-17 11:58:15 +00:00
|
|
|
|
{
|
|
|
|
|
if(spotify.Connect())
|
|
|
|
|
retry = false;
|
|
|
|
|
else
|
|
|
|
|
retry = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this.Close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-01 12:52:33 +00:00
|
|
|
|
mh = spotify.GetMusicHandler();
|
|
|
|
|
eh = spotify.GetEventHandler();
|
|
|
|
|
}
|
|
|
|
|
private async void Form1_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
spotify.Update();
|
|
|
|
|
progressBar1.Maximum = (int)mh.GetCurrentTrack().GetLength() * 100;
|
|
|
|
|
pictureBox1.Image = await spotify.GetMusicHandler().GetCurrentTrack().GetAlbumArtAsync(AlbumArtSize.SIZE_160);
|
|
|
|
|
pictureBox2.Image = await spotify.GetMusicHandler().GetCurrentTrack().GetAlbumArtAsync(AlbumArtSize.SIZE_640);
|
|
|
|
|
|
2014-04-12 21:46:54 +01:00
|
|
|
|
linkLabel1.Text = mh.GetCurrentTrack().GetTrackName();
|
|
|
|
|
linkLabel1.LinkClicked += (senderTwo, args) => Process.Start(mh.GetCurrentTrack().GetTrackURI());
|
|
|
|
|
linkLabel2.Text = mh.GetCurrentTrack().GetArtistName();
|
|
|
|
|
linkLabel2.LinkClicked += (senderTwo, args) => Process.Start(mh.GetCurrentTrack().GetArtistURI());
|
|
|
|
|
linkLabel3.Text = mh.GetCurrentTrack().GetAlbumName();
|
|
|
|
|
linkLabel3.LinkClicked += (senderTwo, args) => Process.Start(mh.GetCurrentTrack().GetAlbumURI());
|
2014-02-01 12:52:33 +00:00
|
|
|
|
|
|
|
|
|
label9.Text = mh.IsPlaying().ToString();
|
|
|
|
|
label11.Text = ((int)(mh.GetVolume() * 100)).ToString();
|
|
|
|
|
label7.Text = mh.IsAdRunning().ToString();
|
|
|
|
|
|
|
|
|
|
eh.OnTrackChange += new SpotifyEventHandler.TrackChangeEventHandler(trackchange);
|
|
|
|
|
eh.OnTrackTimeChange += new SpotifyEventHandler.TrackTimeChangeEventHandler(timechange);
|
|
|
|
|
eh.OnPlayStateChange += new SpotifyEventHandler.PlayStateEventHandler(playstatechange);
|
|
|
|
|
eh.OnVolumeChange += new SpotifyEventHandler.VolumeChangeEventHandler(volumechange);
|
|
|
|
|
eh.SetSynchronizingObject(this);
|
|
|
|
|
eh.ListenForEvents(true);
|
|
|
|
|
}
|
|
|
|
|
private void volumechange(VolumeChangeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
label11.Text = ((int)(mh.GetVolume() * 100)).ToString();
|
|
|
|
|
}
|
|
|
|
|
private void playstatechange(PlayStateEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
label9.Text = e.playing.ToString();
|
|
|
|
|
}
|
|
|
|
|
private async void trackchange(TrackChangeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
progressBar1.Maximum = (int)mh.GetCurrentTrack().GetLength()*100;
|
2014-04-12 21:46:54 +01:00
|
|
|
|
linkLabel1.Text = e.new_track.GetTrackName();
|
|
|
|
|
linkLabel2.Text = e.new_track.GetArtistName();
|
|
|
|
|
linkLabel3.Text = e.new_track.GetAlbumName();
|
2014-02-01 12:52:33 +00:00
|
|
|
|
pictureBox1.Image = await e.new_track.GetAlbumArtAsync(AlbumArtSize.SIZE_160);
|
|
|
|
|
pictureBox2.Image = await e.new_track.GetAlbumArtAsync(AlbumArtSize.SIZE_640);
|
|
|
|
|
label7.Text = mh.IsAdRunning().ToString();
|
|
|
|
|
}
|
|
|
|
|
private void timechange(TrackTimeChangeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
label4.Text = formatTime(e.track_time) + "/" + formatTime(mh.GetCurrentTrack().GetLength());
|
|
|
|
|
progressBar1.Value = (int)e.track_time*100;
|
|
|
|
|
}
|
|
|
|
|
private String formatTime(double sec)
|
|
|
|
|
{
|
|
|
|
|
TimeSpan span = TimeSpan.FromSeconds(sec);
|
|
|
|
|
String secs = span.Seconds.ToString(), mins = span.Minutes.ToString();
|
|
|
|
|
if (secs.Length < 2)
|
|
|
|
|
secs = "0" + secs;
|
|
|
|
|
return mins + ":" + secs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
mh.Play();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button2_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
mh.Pause();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button3_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
mh.Previous();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button4_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
mh.Skip();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button5_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-04-12 21:46:54 +01:00
|
|
|
|
//Not working yet
|
|
|
|
|
//if (SpotifyAPI.IsValidSpotifyURI(textBox1.Text))
|
|
|
|
|
mh.PlayURL(textBox1.Text);
|
2014-02-01 12:52:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void checkBox1_CheckedChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (checkBox1.Checked)
|
|
|
|
|
mh.Mute();
|
|
|
|
|
else
|
|
|
|
|
mh.UnMute();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|