cleaned up controller states. changed to stopped, playing, paused.
This commit is contained in:
parent
caeca57cc9
commit
7d07f26bfa
@ -17,11 +17,10 @@ namespace KaraokePlayer.Classes
|
||||
AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"],
|
||||
BasePath = ConfigurationManager.AppSettings["Firebase.Path"]
|
||||
};
|
||||
private string _state = "play";
|
||||
private string _state = "playing";
|
||||
private IFirebaseClient _client;
|
||||
private ControllerStateChangedEventHandler _stateChanged;
|
||||
private ControllerSongChangedEventHandler _songChanged;
|
||||
private ControllerPlayQueueChangedEventHandler _playQueueChanged;
|
||||
|
||||
private string StatePath
|
||||
{
|
||||
@ -40,20 +39,16 @@ namespace KaraokePlayer.Classes
|
||||
public Song CurrentSong { get; set; }
|
||||
public List<Song> PlayQueue { get; set; }
|
||||
|
||||
public string State {
|
||||
get { return State; }
|
||||
set {
|
||||
_state = value;
|
||||
Update(string.Format("controllers/{0}", this.Id), new { state = _state });
|
||||
}
|
||||
public void SetState(PlayerState state)
|
||||
{
|
||||
Update(string.Format("controllers/{0}", this.Id), new { state = _state });
|
||||
}
|
||||
|
||||
public FirebaseController(string clientId, ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged, ControllerPlayQueueChangedEventHandler playQueueChanged)
|
||||
public FirebaseController(string clientId, ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged)
|
||||
{
|
||||
Id = clientId;
|
||||
_stateChanged = stateChanged;
|
||||
_songChanged = songChanged;
|
||||
_playQueueChanged = playQueueChanged;
|
||||
PlayQueue = new List<Song>();
|
||||
_client = new FirebaseClient(config);
|
||||
Reset();
|
||||
@ -63,19 +58,11 @@ namespace KaraokePlayer.Classes
|
||||
public void NextSong()
|
||||
{
|
||||
Song song = PlayQueue.FirstOrDefault();
|
||||
Stop();
|
||||
if(song != null) { PlaySong(song); }
|
||||
}
|
||||
|
||||
public void PlaySong(Song song) { Update(string.Format("Controllers/{0}", this.Id), new { CurrentSong = song }); }
|
||||
|
||||
public void Next() { this.State = "next"; }
|
||||
|
||||
public void Play() { this.State = "play"; }
|
||||
|
||||
public void Stop() { this.State = "stop"; }
|
||||
|
||||
public void Pause() { this.State = "pause"; }
|
||||
|
||||
private void Delete(string path) { _client.DeleteAsync(path); }
|
||||
|
||||
@ -83,7 +70,6 @@ namespace KaraokePlayer.Classes
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
Stop();
|
||||
Delete(PlayQueuePath);
|
||||
Delete(CurrentSongPath);
|
||||
}
|
||||
@ -105,70 +91,15 @@ namespace KaraokePlayer.Classes
|
||||
await _client.OnAsync(StatePath,
|
||||
added: (s, args, obj) =>
|
||||
{
|
||||
Console.WriteLine("state added");
|
||||
StateDidChange(args.Data);
|
||||
// do we need this? don't think so
|
||||
},
|
||||
changed: (s, args, obj) =>
|
||||
{
|
||||
Console.WriteLine("state changed");
|
||||
Console.WriteLine("state changed " + args.Data);
|
||||
StateDidChange(args.Data);
|
||||
},
|
||||
removed: null
|
||||
);
|
||||
/*
|
||||
await _client.OnAsync(PlayQueuePath,
|
||||
added: (s, args, obj) =>
|
||||
{
|
||||
Console.WriteLine(args.Path);
|
||||
if (args.Path.Contains("Id"))
|
||||
{
|
||||
PlayQueueDidChange();
|
||||
}
|
||||
},
|
||||
changed: (s, args, obj) =>
|
||||
{
|
||||
if (args.Path.Contains("Id"))
|
||||
{
|
||||
PlayQueueDidChange();
|
||||
}
|
||||
},
|
||||
removed: (s, args, obj) =>
|
||||
{
|
||||
PlayQueueDidChange();
|
||||
}
|
||||
);
|
||||
|
||||
await _client.OnAsync(StatePath,
|
||||
added: (s, args, obj) =>
|
||||
{
|
||||
StateDidChange(args.Data);
|
||||
},
|
||||
changed: (s, args, obj) =>
|
||||
{
|
||||
StateDidChange(args.Data);
|
||||
},
|
||||
removed: null
|
||||
);
|
||||
*/
|
||||
}
|
||||
|
||||
private void PlayQueueDidChange()
|
||||
{
|
||||
bool autoPlay = PlayQueue.Count() == 0;
|
||||
var response = _client.Get(PlayQueuePath);
|
||||
var dict = response.ResultAs<Dictionary<string, Song>>();
|
||||
|
||||
PlayQueue.Clear();
|
||||
if (dict != null && dict.Count() > 0)
|
||||
{
|
||||
var array = dict.Values.ToArray();
|
||||
PlayQueue.AddRange(array);
|
||||
if (autoPlay)
|
||||
{
|
||||
NextSong();
|
||||
}
|
||||
}
|
||||
if (_playQueueChanged != null) { _playQueueChanged(); }
|
||||
}
|
||||
|
||||
private void CurrentSongDidChange()
|
||||
@ -184,17 +115,18 @@ namespace KaraokePlayer.Classes
|
||||
|
||||
private void StateDidChange(string state)
|
||||
{
|
||||
if (state == _state) return;
|
||||
_state = state;
|
||||
switch(state)
|
||||
{
|
||||
case "pause":
|
||||
_stateChanged(new ControllerStateChangedEventArgs(PlayerState.Pause));
|
||||
case "paused":
|
||||
_stateChanged(new ControllerStateChangedEventArgs(PlayerState.Paused));
|
||||
break;
|
||||
case "stop":
|
||||
_stateChanged(new ControllerStateChangedEventArgs(PlayerState.Stop));
|
||||
case "stopped":
|
||||
_stateChanged(new ControllerStateChangedEventArgs(PlayerState.Stopped));
|
||||
break;
|
||||
case "play":
|
||||
_stateChanged(new ControllerStateChangedEventArgs(PlayerState.Play));
|
||||
case "playing":
|
||||
_stateChanged(new ControllerStateChangedEventArgs(PlayerState.Playing));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,20 +19,12 @@ namespace KaraokePlayer.Classes
|
||||
{
|
||||
public ControllerSongChangedEventArgs(Song song)
|
||||
{
|
||||
ShouldPlay = true;
|
||||
Song = song;
|
||||
}
|
||||
public ControllerSongChangedEventArgs(bool shouldPlay, Song song)
|
||||
{
|
||||
ShouldPlay = shouldPlay;
|
||||
Song = song;
|
||||
}
|
||||
public Song Song { get; }
|
||||
public bool ShouldPlay { get; }
|
||||
}
|
||||
|
||||
public delegate void ControllerStateChangedEventHandler(ControllerStateChangedEventArgs args);
|
||||
public delegate void ControllerSongChangedEventHandler(ControllerSongChangedEventArgs args);
|
||||
public delegate void ControllerPlayQueueChangedEventHandler();
|
||||
|
||||
}
|
||||
|
||||
@ -8,9 +8,8 @@ namespace Herse.Models
|
||||
{
|
||||
public enum PlayerState
|
||||
{
|
||||
Play,
|
||||
Stop,
|
||||
Pause,
|
||||
Next
|
||||
Playing,
|
||||
Stopped,
|
||||
Paused
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,21 +39,17 @@ namespace KaraokePlayer
|
||||
clientId: ConfigurationManager.AppSettings["KaraokePlayer.ControllerId"],
|
||||
stateChanged: (args) =>
|
||||
{
|
||||
if (args.State == PlayerState.Play)
|
||||
switch (args.State)
|
||||
{
|
||||
this.Invoke(new Action(() => { play(); }));
|
||||
}
|
||||
else if (args.State == PlayerState.Pause)
|
||||
{
|
||||
this.Invoke(new Action(() => { pause(); }));
|
||||
}
|
||||
else if (args.State == PlayerState.Next)
|
||||
{
|
||||
this.Invoke(new Action(() => { next(); }));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Invoke(new Action(() => { stop(); }));
|
||||
case PlayerState.Playing:
|
||||
this.Invoke(new Action(() => { play(); }));
|
||||
break;
|
||||
case PlayerState.Paused:
|
||||
this.Invoke(new Action(() => { pause(); }));
|
||||
break;
|
||||
case PlayerState.Stopped:
|
||||
this.Invoke(new Action(() => { stop(); }));
|
||||
break;
|
||||
}
|
||||
},
|
||||
songChanged: (args) =>
|
||||
@ -61,9 +57,7 @@ namespace KaraokePlayer
|
||||
this.Invoke(new Action(() => { stop(); }));
|
||||
currentSong = args.Song;
|
||||
this.Invoke(new Action(() => { previewSong(); }));
|
||||
},
|
||||
playQueueChanged: null
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private async void previewSong()
|
||||
@ -72,6 +66,7 @@ namespace KaraokePlayer
|
||||
songInfoForm.Show();
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
play();
|
||||
controller.SetState(PlayerState.Playing);
|
||||
songInfoForm.Hide();
|
||||
}
|
||||
|
||||
@ -104,49 +99,65 @@ namespace KaraokePlayer
|
||||
currentPlayer.Resume();
|
||||
return;
|
||||
}
|
||||
if (currentSong.FileType == FileType.CDG)
|
||||
switch(currentSong.FileType)
|
||||
{
|
||||
case FileType.CDG:
|
||||
PlayCdg();
|
||||
break;
|
||||
case FileType.ZIP:
|
||||
PlayZip();
|
||||
break;
|
||||
case FileType.MP4:
|
||||
PlayM4p();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayM4p()
|
||||
{
|
||||
currentPlayer = karaokeMP4Player;
|
||||
karaokeMP4Player.Play(new Uri(currentSong.Path));
|
||||
karaokeMP4Player.Visible = true;
|
||||
karaokeCDGPlayer.Visible = false;
|
||||
karaokeCDGPlayer.Stop();
|
||||
}
|
||||
|
||||
private void PlayZip()
|
||||
{
|
||||
string playPath = null;
|
||||
string extractPath = Path.Combine(Directory.GetCurrentDirectory(), "temp");
|
||||
DeleteDirectory(extractPath);
|
||||
Directory.CreateDirectory(extractPath);
|
||||
using (ZipArchive archive = ZipFile.OpenRead(currentSong.Path))
|
||||
{
|
||||
foreach (ZipArchiveEntry entry in archive.Entries)
|
||||
{
|
||||
if (entry.FullName.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
playPath = Path.Combine(extractPath, entry.FullName);
|
||||
}
|
||||
entry.ExtractToFile(Path.Combine(extractPath, entry.FullName));
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrEmpty(playPath))
|
||||
{
|
||||
currentPlayer = karaokeCDGPlayer;
|
||||
karaokeCDGPlayer.Play(new Uri(Path.ChangeExtension(currentSong.Path, ".mp3")));
|
||||
karaokeCDGPlayer.Play(new Uri(playPath));
|
||||
karaokeCDGPlayer.Visible = true;
|
||||
karaokeMP4Player.Visible = false;
|
||||
karaokeMP4Player.Stop();
|
||||
}
|
||||
else if (currentSong.FileType == FileType.ZIP)
|
||||
{
|
||||
string playPath = null;
|
||||
string extractPath = Path.Combine(Directory.GetCurrentDirectory(), "temp");
|
||||
DeleteDirectory(extractPath);
|
||||
Directory.CreateDirectory(extractPath);
|
||||
using (ZipArchive archive = ZipFile.OpenRead(currentSong.Path))
|
||||
{
|
||||
foreach (ZipArchiveEntry entry in archive.Entries)
|
||||
{
|
||||
if (entry.FullName.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
playPath = Path.Combine(extractPath, entry.FullName);
|
||||
}
|
||||
entry.ExtractToFile(Path.Combine(extractPath, entry.FullName));
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrEmpty(playPath))
|
||||
{
|
||||
currentPlayer = karaokeCDGPlayer;
|
||||
karaokeCDGPlayer.Play(new Uri(playPath));
|
||||
karaokeCDGPlayer.Visible = true;
|
||||
karaokeMP4Player.Visible = false;
|
||||
karaokeMP4Player.Stop();
|
||||
}
|
||||
}
|
||||
else {
|
||||
currentPlayer = karaokeMP4Player;
|
||||
karaokeMP4Player.Play(new Uri(currentSong.Path));
|
||||
karaokeMP4Player.Visible = true;
|
||||
karaokeCDGPlayer.Visible = false;
|
||||
karaokeCDGPlayer.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void PlayCdg()
|
||||
{
|
||||
currentPlayer = karaokeCDGPlayer;
|
||||
karaokeCDGPlayer.Play(new Uri(Path.ChangeExtension(currentSong.Path, ".mp3")));
|
||||
karaokeCDGPlayer.Visible = true;
|
||||
karaokeMP4Player.Visible = false;
|
||||
karaokeMP4Player.Stop();
|
||||
}
|
||||
|
||||
private void DeleteDirectory(string target_dir)
|
||||
{
|
||||
if (!Directory.Exists(target_dir)) { return; }
|
||||
|
||||
Loading…
Reference in New Issue
Block a user