KaraokePC/KaraokePlayer/MainForm.cs

182 lines
5.9 KiB
C#

using System;
using MaterialSkin;
using MaterialSkin.Controls;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.IO.Compression;
using System.Windows.Forms;
using KaraokePlayer.Classes;
using System.Threading.Tasks;
using System.Configuration;
using Herse.Models;
namespace KaraokePlayer
{
public partial class MainForm : Form
{
private SongInfoForm songInfoForm = new SongInfoForm();
private delegate void Action();
private KaraokeVideoPlayer currentPlayer = null;
private FirebaseController controller;
private Song currentSong;
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);
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"],
stateChanged: (args) =>
{
switch (args.State)
{
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) =>
{
this.Invoke(new Action(() => { stop(); }));
currentSong = args.Song;
this.Invoke(new Action(() => { previewSong(); }));
});
}
private async void previewSong()
{
songInfoForm.Update(currentSong);
songInfoForm.Show();
await Task.Delay(TimeSpan.FromSeconds(5));
play();
controller.SetState(PlayerState.Playing);
songInfoForm.Hide();
}
private void karaokePlayerSongEnded(object sender, EventArgs e)
{
next();
}
private void next()
{
controller.NextSong();
}
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);
}
}
}