KaraokePC/KaraokePlayer/MainForm.cs

172 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) =>
{
if (args.State == PlayerState.Play)
{
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(); }));
}
},
songChanged: (args) =>
{
this.Invoke(new Action(() => { stop(); }));
currentSong = args.Song;
this.Invoke(new Action(() => { previewSong(); }));
},
playQueueChanged: null
);
}
private async void previewSong()
{
songInfoForm.Update(currentSong);
songInfoForm.Show();
await Task.Delay(TimeSpan.FromSeconds(5));
//controller.Play();
play();
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;
}
if (currentSong.FileType == FileType.CDG)
{
currentPlayer = karaokeCDGPlayer;
karaokeCDGPlayer.Play(new Uri(Path.ChangeExtension(currentSong.Path, ".mp3")));
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 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);
}
}
}