From 89254a245e307ba35354d72baaa4520e44f27ba7 Mon Sep 17 00:00:00 2001 From: Don Archer Date: Fri, 19 Aug 2016 18:46:57 -0700 Subject: [PATCH] fixed a few things --- FirebaseKaraoke/FirebaseController.cs | 48 +++++++++++++++++++-------- Herse.Models/Herse.Models.csproj | 1 + Herse.Models/Settings.cs | 15 +++++++++ KaraokePlayer/App.config | 2 +- KaraokePlayer/MainForm.Designer.cs | 10 ++++-- KaraokePlayer/MainForm.cs | 32 +++++++++++------- KaraokePlayer/PlayerWrapper.cs | 9 ++++- SongCrawler/App.config | 4 +++ SongCrawler/Program.cs | 29 +++++++++++----- SongCrawler/SongCrawler.csproj | 1 + 10 files changed, 111 insertions(+), 40 deletions(-) create mode 100644 Herse.Models/Settings.cs diff --git a/FirebaseKaraoke/FirebaseController.cs b/FirebaseKaraoke/FirebaseController.cs index 1be1b72..626d19e 100644 --- a/FirebaseKaraoke/FirebaseController.cs +++ b/FirebaseKaraoke/FirebaseController.cs @@ -21,7 +21,12 @@ namespace KaraokePlayer.Classes private IFirebaseClient _client; private ControllerStateChangedEventHandler _stateChanged; private ControllerSongChangedEventHandler _songChanged; + public Settings Settings { get; set; } + private string SettingsPath + { + get { return string.Format("controllers/{0}/player/settings", this.Id); } + } private string QueuePath { get { return string.Format("controllers/{0}/player/queue", this.Id); } @@ -38,20 +43,6 @@ namespace KaraokePlayer.Classes public string Id { get; set; } public Song CurrentSong { get; set; } - public void EndSong() - { - var response = _client.Get(QueuePath); - List queue = response.ResultAs>(); - queue.RemoveAt(0); - _client.Set(QueuePath, queue); - } - - public void SetState(PlayerState state) - { - _state = state; - _client.SetAsync(StatePath, "Playing"); - } - public FirebaseController(string clientId, ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged) { Id = clientId; @@ -59,6 +50,7 @@ namespace KaraokePlayer.Classes _songChanged = songChanged; _client = new FirebaseClient(config); SetupListener(); + Settings = _client.Get(SettingsPath).ResultAs(); } private void SetupListener() @@ -90,6 +82,30 @@ namespace KaraokePlayer.Classes ); } + /// + /// called by the player to end the current song in firebase + /// + public void EndSong() + { + SetState(PlayerState.Stopped); + List queue = _client.Get(QueuePath).ResultAs>(); + queue.RemoveAt(0); + _client.Set(QueuePath, queue); + } + + /// + /// called by the player to set the state in firebase + /// + /// + public void SetState(PlayerState state) + { + _state = state; + _client.SetAsync(StatePath, "Playing"); + } + + /// + /// called by firebase to change the current song + /// private void CurrentSongDidChange() { var response = _client.Get(CurrentSongPath); @@ -102,6 +118,10 @@ namespace KaraokePlayer.Classes } } + /// + /// called by firebase to change the current state + /// + /// private void StateDidChange(PlayerState newstate) { if (newstate == _state) return; diff --git a/Herse.Models/Herse.Models.csproj b/Herse.Models/Herse.Models.csproj index cdf700d..5880b0c 100644 --- a/Herse.Models/Herse.Models.csproj +++ b/Herse.Models/Herse.Models.csproj @@ -48,6 +48,7 @@ + diff --git a/Herse.Models/Settings.cs b/Herse.Models/Settings.cs new file mode 100644 index 0000000..8d849dc --- /dev/null +++ b/Herse.Models/Settings.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace Herse.Models +{ + public class Settings + { + [JsonProperty("autoadvance")] + public bool AutoAdvance { get; set; } + } +} diff --git a/KaraokePlayer/App.config b/KaraokePlayer/App.config index 8520856..8a11510 100644 --- a/KaraokePlayer/App.config +++ b/KaraokePlayer/App.config @@ -8,7 +8,7 @@ - + diff --git a/KaraokePlayer/MainForm.Designer.cs b/KaraokePlayer/MainForm.Designer.cs index 8f06750..93ef29e 100644 --- a/KaraokePlayer/MainForm.Designer.cs +++ b/KaraokePlayer/MainForm.Designer.cs @@ -37,29 +37,33 @@ this.karaokeCDGPlayer.Dock = System.Windows.Forms.DockStyle.Fill; this.karaokeCDGPlayer.isCDG = true; this.karaokeCDGPlayer.Location = new System.Drawing.Point(0, 0); + this.karaokeCDGPlayer.Margin = new System.Windows.Forms.Padding(3, 53, 3, 3); this.karaokeCDGPlayer.Name = "karaokeCDGPlayer"; - this.karaokeCDGPlayer.Size = new System.Drawing.Size(506, 296); + this.karaokeCDGPlayer.Size = new System.Drawing.Size(796, 484); this.karaokeCDGPlayer.TabIndex = 0; // // karaokeMP4Player // + this.karaokeMP4Player.AutoSize = true; this.karaokeMP4Player.Dock = System.Windows.Forms.DockStyle.Fill; this.karaokeMP4Player.isCDG = false; this.karaokeMP4Player.Location = new System.Drawing.Point(0, 0); + this.karaokeMP4Player.Margin = new System.Windows.Forms.Padding(3, 53, 3, 3); this.karaokeMP4Player.Name = "karaokeMP4Player"; - this.karaokeMP4Player.Size = new System.Drawing.Size(506, 296); + this.karaokeMP4Player.Size = new System.Drawing.Size(796, 484); this.karaokeMP4Player.TabIndex = 0; // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(506, 296); + this.ClientSize = new System.Drawing.Size(796, 484); this.Controls.Add(this.karaokeMP4Player); this.Controls.Add(this.karaokeCDGPlayer); this.Name = "MainForm"; this.Text = "Karaoke Player"; this.ResumeLayout(false); + this.PerformLayout(); } diff --git a/KaraokePlayer/MainForm.cs b/KaraokePlayer/MainForm.cs index 3653bb6..cc73a67 100644 --- a/KaraokePlayer/MainForm.cs +++ b/KaraokePlayer/MainForm.cs @@ -21,11 +21,6 @@ namespace KaraokePlayer private FirebaseController controller; private PlayerWrapper player; - public void SongEnded() - { - controller.EndSong(); - } - public MainForm() { InitializeComponent(); @@ -35,16 +30,20 @@ namespace KaraokePlayer this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; this.ShowInTaskbar = true; - + string controllerId = ConfigurationManager.AppSettings["KaraokePlayer.ControllerId"]; + //labelParty.Text = string.Format("https://herse.firebaseapp.com/ party: {0}", controllerId); controller = new FirebaseController( - clientId: ConfigurationManager.AppSettings["KaraokePlayer.ControllerId"], + clientId: controllerId, stateChanged: (args) => { switch (args.State) { case PlayerState.Playing: - this.Invoke(new Action(() => { player.play(); })); + this.Invoke(new Action(() => { + songInfoForm.Hide(); + player.play(); + })); break; case PlayerState.Paused: this.Invoke(new Action(() => { player.pause(); })); @@ -61,16 +60,25 @@ namespace KaraokePlayer this.Invoke(new Action(() => { previewSong(); })); } ); + controller.SetState(PlayerState.Stopped); + } + + public void SongEnded() + { + controller.EndSong(); } private async void previewSong() { songInfoForm.Update(player.Song); songInfoForm.Show(); - await Task.Delay(TimeSpan.FromSeconds(5)); - player.play(); - controller.SetState(PlayerState.Playing); - songInfoForm.Hide(); + if(controller.Settings == null || controller.Settings.AutoAdvance) + { + await Task.Delay(TimeSpan.FromSeconds(10)); + player.play(); + controller.SetState(PlayerState.Playing); + songInfoForm.Hide(); + } } } } diff --git a/KaraokePlayer/PlayerWrapper.cs b/KaraokePlayer/PlayerWrapper.cs index f651a20..52a4660 100644 --- a/KaraokePlayer/PlayerWrapper.cs +++ b/KaraokePlayer/PlayerWrapper.cs @@ -85,7 +85,14 @@ namespace KaraokePlayer { string playPath = null; string extractPath = Path.Combine(Directory.GetCurrentDirectory(), "temp"); - DeleteDirectory(extractPath); + try + { + DeleteDirectory(extractPath); + } + catch(Exception) + { + // do nothing + } Directory.CreateDirectory(extractPath); using (ZipArchive archive = ZipFile.OpenRead(Song.Path)) { diff --git a/SongCrawler/App.config b/SongCrawler/App.config index 71a06ba..8444849 100644 --- a/SongCrawler/App.config +++ b/SongCrawler/App.config @@ -1,5 +1,9 @@  + + + + diff --git a/SongCrawler/Program.cs b/SongCrawler/Program.cs index 4fb9fa1..d30778e 100644 --- a/SongCrawler/Program.cs +++ b/SongCrawler/Program.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO.Compression; +using System.Configuration; namespace SongCrawler { @@ -16,22 +17,32 @@ namespace SongCrawler { static void Main(string[] args) { + if(args.Length != 2) + { + Console.WriteLine("usage: songcrawler partyid songspath"); + return; + } string controller = args[0]; string songpath = args[1]; IFirebaseConfig config = new FirebaseConfig { - AuthSecret = "9BQHUEJhScgK2FP10hvlToxTlGQpXT94Cvx01piO", - BasePath = "https://herse.firebaseio.com/" + AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"], + BasePath = ConfigurationManager.AppSettings["Firebase.Path"] }; FireSharp.FirebaseClient client = new FireSharp.FirebaseClient(config); string firepath = string.Format("controllers/{0}/songs", controller); + Console.WriteLine("Loading current library"); + List songs = client.Get(firepath).ResultAs>(); + if (songs != null) + Console.WriteLine(string.Format("{0} songs loaded", songs.Count)); + else + songs = new List(); List files = new List(); files.AddRange(FindFiles("mp3", songpath)); files.AddRange(FindFiles("zip", songpath)); files.AddRange(FindFiles("mp4", songpath)); - - List songs = new List(); + Song song = null; int i = 0; foreach (string filepath in files) @@ -41,14 +52,14 @@ namespace SongCrawler { song = MakeSong(filepath); Console.WriteLine(string.Format("{0:000000}/{1} - {2}", i, files.Count, song.Title)); - songs.Add(song); + if(!songs.Any(s => s.Path == song.Path)) songs.Add(song); } catch(Exception ex) { Console.WriteLine(ex.Message); } } - SetResponse response = client.Set(firepath, songs); + client.Set(firepath, songs); } private static Song MakeSong(string filepath) @@ -113,9 +124,9 @@ namespace SongCrawler try { tagFile = TagLib.File.Create(path); - song.Title = tagFile.Tag.Title; - song.Artist = tagFile.Tag.FirstPerformer; - song.Genre = tagFile.Tag.FirstGenre; + song.Title = tagFile.Tag.Title.Trim(); + song.Artist = tagFile.Tag.FirstPerformer.Trim(); + song.Genre = tagFile.Tag.FirstGenre.Trim(); song.Year = (int)tagFile.Tag.Year; } catch diff --git a/SongCrawler/SongCrawler.csproj b/SongCrawler/SongCrawler.csproj index e42ec59..ba68b0a 100644 --- a/SongCrawler/SongCrawler.csproj +++ b/SongCrawler/SongCrawler.csproj @@ -54,6 +54,7 @@ True +