moved player node outside of songs for speed

This commit is contained in:
Don Archer 2016-08-07 16:33:27 -07:00
parent b6588ff28d
commit 0097334902
5 changed files with 85 additions and 54 deletions

View File

@ -17,18 +17,22 @@ namespace KaraokePlayer.Classes
AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"],
BasePath = ConfigurationManager.AppSettings["Firebase.Path"]
};
private PlayerState _state = PlayerState.playing;
private PlayerState _state = PlayerState.Playing;
private IFirebaseClient _client;
private ControllerStateChangedEventHandler _stateChanged;
private ControllerSongChangedEventHandler _songChanged;
private string QueuePath
{
get { return string.Format("controllers/{0}/player/queue", this.Id); }
}
private string StatePath
{
get { return string.Format("controllers/{0}/state/", this.Id); }
get { return string.Format("controllers/{0}/player/state/", this.Id); }
}
private string CurrentSongPath
{
get { return string.Format("controllers/{0}/queue/0/", this.Id); }
get { return string.Format("controllers/{0}/player/queue/0/", this.Id); }
}
public string Id { get; set; }
@ -36,17 +40,16 @@ namespace KaraokePlayer.Classes
public void EndSong()
{
//_client.DeleteAsync(CurrentSongPath.TrimEnd('/'));
var response = _client.Get(string.Format("controllers/{0}/queue", this.Id));
var response = _client.Get(QueuePath);
List<QueueItem> queue = response.ResultAs<List<QueueItem>>();
queue.RemoveAt(0);
_client.Set(string.Format("controllers/{0}/queue", this.Id), queue);
_client.Set(QueuePath, queue);
}
public void SetState(PlayerState state)
{
_state = state;
_client.SetAsync(StatePath, "playing");
_client.SetAsync(StatePath, "Playing");
}
public FirebaseController(string clientId, ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged)
@ -80,7 +83,7 @@ namespace KaraokePlayer.Classes
changed: (s, args, obj) =>
{
Console.WriteLine("state changed " + args.Data);
PlayerState newstate = (PlayerState)Enum.Parse(typeof(PlayerState), args.Data);
PlayerState newstate = (PlayerState)Enum.Parse(typeof(PlayerState), args.Data, true);
StateDidChange(newstate);
},
removed: null
@ -89,7 +92,7 @@ namespace KaraokePlayer.Classes
private void CurrentSongDidChange()
{
var response = _client.Get(CurrentSongPath);
var response = _client.Get(CurrentSongPath);
var item = response.ResultAs<QueueItem>();
if (item == null) return;
if (CurrentSong == null || CurrentSong.Path != item.Song.Path)

View File

@ -8,8 +8,8 @@ namespace Herse.Models
{
public enum PlayerState
{
playing,
stopped,
paused
Playing,
Stopped,
Paused
}
}

View File

@ -8,7 +8,7 @@
<appSettings>
<add key="Firebase.Secret" value="9BQHUEJhScgK2FP10hvlToxTlGQpXT94Cvx01piO" />
<add key="Firebase.Path" value="https://herse.firebaseio.com/" />
<add key="KaraokePlayer.ControllerId" value="archer1" />
<add key="KaraokePlayer.ControllerId" value="archer" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />

View File

@ -33,7 +33,7 @@ namespace KaraokePlayer
player.OnSongEnded += SongEnded;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//this.WindowState = FormWindowState.Maximized;
this.WindowState = FormWindowState.Maximized;
this.ShowInTaskbar = true;
@ -43,13 +43,13 @@ namespace KaraokePlayer
{
switch (args.State)
{
case PlayerState.playing:
case PlayerState.Playing:
this.Invoke(new Action(() => { player.play(); }));
break;
case PlayerState.paused:
case PlayerState.Paused:
this.Invoke(new Action(() => { player.pause(); }));
break;
case PlayerState.stopped:
case PlayerState.Stopped:
this.Invoke(new Action(() => { player.stop(); }));
break;
}
@ -69,7 +69,7 @@ namespace KaraokePlayer
songInfoForm.Show();
await Task.Delay(TimeSpan.FromSeconds(5));
player.play();
controller.SetState(PlayerState.playing);
controller.SetState(PlayerState.Playing);
songInfoForm.Hide();
}
}

View File

@ -27,32 +27,58 @@ namespace SongCrawler
string firepath = string.Format("controllers/{0}/songs", controller);
List<string> files = new List<string>();
files.AddRange(Directory.GetFiles(songpath, "*.mp3", SearchOption.AllDirectories));
files.AddRange(Directory.GetFiles(songpath, "*.zip", SearchOption.AllDirectories));
files.AddRange(FindFiles("mp3", songpath));
files.AddRange(FindFiles("zip", songpath));
files.AddRange(FindFiles("mp4", songpath));
List<Song> songs = new List<Song>();
string id3path = null;
Song song;
Song song = null;
int i = 0;
foreach (string filepath in files)
{
Console.WriteLine(filepath);
var ext = Path.GetExtension(filepath).ToLower();
switch (ext)
i++;
try
{
case ".mp3":
id3path = filepath;
break;
case ".zip":
id3path = UnzipMp3(filepath);
break;
song = MakeSong(filepath);
Console.WriteLine(string.Format("{0:000000}/{1} - {2}", i, files.Count, song.Title));
songs.Add(song);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
song = ReadId3(id3path);
CheckTitle(song);
song.Path = filepath;
songs.Add(song);
}
SetResponse response = client.Set(firepath, songs);
}
private static Song MakeSong(string filepath)
{
Song song = null;
var ext = Path.GetExtension(filepath).ToLower();
switch (ext)
{
case ".mp3":
case ".mp4":
song = ReadId3(filepath);
break;
case ".zip":
song = ReadId3FromZip(filepath);
break;
}
CheckTitle(song);
song.Path = filepath;
return song;
}
private static string[] FindFiles(string ext, string path)
{
Console.Write(string.Format("\rscanning {0} for {1} - ", path, ext));
string[] files = Directory.GetFiles(path, "*." + ext, SearchOption.AllDirectories);
Console.WriteLine(string.Format("{0} found", files.Length));
return files;
}
private static void CheckTitle(Song song)
{
if(string.IsNullOrEmpty(song.Title))
@ -71,31 +97,33 @@ namespace SongCrawler
}
}
private static string UnzipMp3(string filepath)
private static Song ReadId3FromZip(string zippath)
{
string file = Path.GetFileNameWithoutExtension(filepath);
try
{
ZipFile.ExtractToDirectory(filepath, "c:\\temp");
}
catch
{
// do nothing fancy
}
return "c:\\temp\\" + file + ".mp3";
ZipFile.ExtractToDirectory(zippath, "c:\\temp");
string filepath = Directory.GetFiles("c:\\temp", "*.mp3")[0];
Song song = ReadId3(filepath);
foreach (string file in Directory.GetFiles("c:\\temp")) File.Delete(file);
return song;
}
static Song ReadId3(string path)
{
TagLib.File tagFile = TagLib.File.Create(path);
return new Song()
Song song = new Song();
TagLib.File tagFile;
try
{
Title = tagFile.Tag.Title,
Artist = tagFile.Tag.FirstPerformer,
Path = path,
Genre = tagFile.Tag.FirstGenre,
Year = (int)tagFile.Tag.Year
};
tagFile = TagLib.File.Create(path);
song.Title = tagFile.Tag.Title;
song.Artist = tagFile.Tag.FirstPerformer;
song.Genre = tagFile.Tag.FirstGenre;
song.Year = (int)tagFile.Tag.Year;
}
catch
{
// do nothing;
}
song.Path = path;
return song;
}
}
}