abstracted all the two player nonsense into a wrapped class

This commit is contained in:
Don Archer 2016-08-04 22:17:05 -07:00
parent e003e9c548
commit d1172c8843
3 changed files with 152 additions and 116 deletions

View File

@ -111,6 +111,7 @@
<Compile Include="MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="PlayerWrapper.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SongInfoForm.cs">

View File

@ -18,22 +18,18 @@ namespace KaraokePlayer
{
private SongInfoForm songInfoForm = new SongInfoForm();
private delegate void Action();
private KaraokeVideoPlayer currentPlayer = null;
private FirebaseController controller;
private Song currentSong;
private PlayerWrapper player;
public MainForm()
{
InitializeComponent();
karaokeCDGPlayer.setup(true, true);
karaokeMP4Player.setup(false, true);
karaokeCDGPlayer.songEndedHandler += new KaraokePlayer.KaraokeVideoPlayer.SongEndedEventHandler(this.karaokePlayerSongEnded);
karaokeMP4Player.songEndedHandler += new KaraokePlayer.KaraokeVideoPlayer.SongEndedEventHandler(this.karaokePlayerSongEnded);
player = new PlayerWrapper(karaokeCDGPlayer, karaokeMP4Player);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//this.WindowState = FormWindowState.Maximized;
this.ShowInTaskbar = true;
karaokeCDGPlayer.Dock = DockStyle.Fill;
karaokeMP4Player.Dock = DockStyle.Fill;
controller = new FirebaseController(
clientId: ConfigurationManager.AppSettings["KaraokePlayer.ControllerId"],
@ -42,136 +38,38 @@ namespace KaraokePlayer
switch (args.State)
{
case PlayerState.Playing:
this.Invoke(new Action(() => { play(); }));
this.Invoke(new Action(() => { player.play(); }));
break;
case PlayerState.Paused:
this.Invoke(new Action(() => { pause(); }));
this.Invoke(new Action(() => { player.pause(); }));
break;
case PlayerState.Stopped:
this.Invoke(new Action(() => { stop(); }));
this.Invoke(new Action(() => { player.stop(); }));
break;
}
},
songChanged: (args) =>
{
this.Invoke(new Action(() => { stop(); }));
currentSong = args.Song;
this.Invoke(new Action(() => { player.stop(); }));
player.Song = args.Song;
this.Invoke(new Action(() => { previewSong(); }));
});
}
private async void previewSong()
{
songInfoForm.Update(currentSong);
songInfoForm.Update(player.Song);
songInfoForm.Show();
await Task.Delay(TimeSpan.FromSeconds(5));
play();
player.play();
controller.SetState(PlayerState.Playing);
songInfoForm.Hide();
}
private void karaokePlayerSongEnded(object sender, EventArgs e)
{
//TODO: figure out how to play the next song. probably just delete the current one
}
private void stop()
{
if (currentPlayer == null) return;
currentPlayer.Stop();
}
private void pause()
{
if (currentPlayer == null) return;
currentPlayer.Pause();
}
private void play()
{
if (currentSong == null) return;
if (currentPlayer != null && currentPlayer.isPaused)
{
currentPlayer.Resume();
return;
}
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(playPath));
karaokeCDGPlayer.Visible = true;
karaokeMP4Player.Visible = false;
karaokeMP4Player.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; }
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(target_dir, false);
}
}
}

View File

@ -0,0 +1,137 @@
using KaraokePlayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using Herse.Models;
using System.IO.Compression;
namespace KaraokePlayer
{
public class PlayerWrapper
{
private KaraokeVideoPlayer currentPlayer = null;
private KaraokeVideoPlayer mp4Player;
private KaraokeVideoPlayer cdgPlayer;
public Song Song { get; set; }
public PlayerWrapper(KaraokeVideoPlayer cdgPlayer, KaraokeVideoPlayer mp4Player)
{
cdgPlayer.setup(true, true);
mp4Player.setup(false, true);
cdgPlayer.songEndedHandler += new KaraokePlayer.KaraokeVideoPlayer.SongEndedEventHandler(this.karaokePlayerSongEnded);
mp4Player.songEndedHandler += new KaraokePlayer.KaraokeVideoPlayer.SongEndedEventHandler(this.karaokePlayerSongEnded);
cdgPlayer.Dock = DockStyle.Fill;
mp4Player.Dock = DockStyle.Fill;
this.mp4Player = mp4Player;
this.cdgPlayer = cdgPlayer;
}
private void karaokePlayerSongEnded(object sender, EventArgs e)
{
//TODO: figure out how to play the next song. probably just delete the current one
}
public void stop()
{
if (currentPlayer == null) return;
currentPlayer.Stop();
}
public void pause()
{
if (currentPlayer == null) return;
currentPlayer.Pause();
}
public void play()
{
if (Song == null) return;
if (currentPlayer != null && currentPlayer.isPaused)
{
currentPlayer.Resume();
return;
}
switch (Song.FileType)
{
case FileType.CDG:
PlayCdg();
break;
case FileType.ZIP:
PlayZip();
break;
case FileType.MP4:
PlayM4p();
break;
}
}
private void PlayM4p()
{
currentPlayer = mp4Player;
mp4Player.Play(new Uri(Song.Path));
mp4Player.Visible = true;
cdgPlayer.Visible = false;
cdgPlayer.Stop();
}
private void PlayZip()
{
string playPath = null;
string extractPath = Path.Combine(Directory.GetCurrentDirectory(), "temp");
DeleteDirectory(extractPath);
Directory.CreateDirectory(extractPath);
using (ZipArchive archive = ZipFile.OpenRead(Song.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 = cdgPlayer;
cdgPlayer.Play(new Uri(playPath));
cdgPlayer.Visible = true;
mp4Player.Visible = false;
mp4Player.Stop();
}
}
private void PlayCdg()
{
currentPlayer = cdgPlayer;
cdgPlayer.Play(new Uri(Path.ChangeExtension(Song.Path, ".mp3")));
cdgPlayer.Visible = true;
mp4Player.Visible = false;
mp4Player.Stop();
}
private void DeleteDirectory(string target_dir)
{
if (!Directory.Exists(target_dir)) { return; }
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(target_dir, false);
}
}
}