Files
soundboard/SoundBoard/mainForm.cs
T

466 lines
20 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Reflection;
namespace SoundBoard
{
public partial class formMain : Form
{
public WMPLib.WindowsMediaPlayer Player = new WMPLib.WindowsMediaPlayer();
public Random randomNum = new Random();
public ListBox listBoxEntity = new ListBox();
ProcessStartInfo process = new ProcessStartInfo();
BackgroundWorker worker = new BackgroundWorker();
public string _workingDirectory;
public string[] _soundFiles;
public string[] _soundDirectories;
public formMain()
{
InitializeComponent();
}
private void formMain_Load(object sender, EventArgs e)
{
_workingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
_soundFiles = Directory.GetFiles(_workingDirectory);
foreach (string sound in _soundFiles)
{
CreateUITabs("Working Directory", _workingDirectory);
}
_soundDirectories = Directory.GetDirectories(_workingDirectory);
foreach (string sounds in _soundDirectories)
{
CreateUITabs(Path.GetFileName(sounds), sounds);
}
worker.WorkerSupportsCancellation = true;
worker.WorkerReportsProgress = true;
fileSystemWatcherMain.Path = _workingDirectory;
fileSystemWatcherMain.IncludeSubdirectories = true;
fileSystemWatcherMain.Created += new FileSystemEventHandler(fileSystemUpdateDetected); //file or folder created
fileSystemWatcherMain.Deleted += new FileSystemEventHandler(fileSystemUpdateDetected); //file or folder moved or deleted
fileSystemWatcherMain.Renamed += new RenamedEventHandler(fileSystemUpdateDetected); //for any file changes like file extension changes
soundboardTabControl.SelectedIndexChanged += new EventHandler(CheckForDisabledListBox); //disables ore enables the Show on Disk file menu object
Player.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(WMP_PlayStateChange);
//check to see if the listBox is empty on form load
if (soundboardTabControl.SelectedTab.Controls[0].Enabled == true)
{
mainMenuFileShowOnDisk.Enabled = true;
btnRandom.Enabled = true;
}
else
{
mainMenuFileShowOnDisk.Enabled = false;
btnRandom.Enabled = false;
}
}
private void WMP_PlayStateChange(int newState)
{
}
private void fileSystemUpdateDetected(object sender, FileSystemEventArgs e)
{
//perform the UI refresh operation
RefreshUIControls();
}
private void CheckForDisabledListBox(object sender, EventArgs e)
{
ListBox listBoxEntity = new ListBox();
try
{
listBoxEntity = (ListBox)soundboardTabControl.SelectedTab.Controls[0];
}
catch (Exception spam)
{
return;
}
if (listBoxEntity.Enabled == false)
{
mainMenuFileShowOnDisk.Enabled = false;
btnRandom.Enabled = false;
}
else
{
mainMenuFileShowOnDisk.Enabled = true;
btnRandom.Enabled = true;
}
}
private void RefreshUIControls()
{
_workingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
_soundDirectories = Directory.GetDirectories(_workingDirectory);
foreach (TabPage tabPage in soundboardTabControl.TabPages)
{
foreach (ListBox listBox in tabPage.Controls)
{
listBox.Dispose();
}
tabPage.Dispose();
}
foreach (string sounds in _soundFiles)
{
CreateUITabs("Working Directory", _workingDirectory);
}
foreach (string files in _soundDirectories)
{
CreateUITabs(Path.GetFileName(files), files);
}
//check to see if the listBox is empty
if (soundboardTabControl.SelectedTab.Controls[0].Enabled == true)
{
mainMenuFileShowOnDisk.Enabled = true;
btnRandom.Enabled = true;
}
else
{
mainMenuFileShowOnDisk.Enabled = false;
btnRandom.Enabled = false;
}
}
private void CreateUITabs(string tabName, string directoryPath)
{
//add tabs dynamically
int soundControlHeight = soundboardTabControl.Height;
int soundControlWidth = soundboardTabControl.Width;
string[] directoryFiles; //files in the target directory
TabPage tab = new TabPage();
ListBox localListBox = new ListBox();
if (soundboardTabControl.Controls.ContainsKey(tabName.Replace(" ", "") + "Tab"))
{
}
else
{
tab.Text = tabName; //display the folder's full name to the user
tab.Name = tabName.Replace(" ", "") + "Tab"; //create a legal name for the tab's (name) property
tab.Height = soundControlHeight;
tab.Height = soundControlWidth;
soundboardTabControl.Controls.Add(tab);
localListBox.Height = soundControlHeight - 25;
localListBox.Width = soundControlWidth - 5;
localListBox.Name = tabName.Replace(" ", "") + "ListBox";
tab.Controls.Add(localListBox);
localListBox.Click += new EventHandler((sender, e) => ListBoxOnLeftClick(sender, e, localListBox)); //create event handler for the lsit box and pass the object in
}
directoryFiles = Directory.GetFiles(Path.GetFullPath(directoryPath));
foreach (string files in directoryFiles)
{
switch (Path.GetExtension(files))//check to make sure the file is a sound file
{
case ".wav":
case ".mp3":
localListBox.Items.Add(Path.GetFileNameWithoutExtension(files));
break;
default:
break;
}
}
if (localListBox.Items.Count == 0)
{
localListBox.Items.Add("No sound files found in the " + tabName + " directory.");
localListBox.Enabled = false;
if (soundboardTabControl.SelectedIndex == 0)
{
mainMenuFileShowOnDisk.Enabled = false;
btnRandom.Enabled = false;
}
}
}
private void ListBoxOnLeftClick(object sender, EventArgs e, ListBox list)
{
//check to make sure something is actually selected
if (list.SelectedItem == null)
{
return;
}
//get the file's location on disk, double back if it's not a .wav file
string fileLocation;
WMPLib.IWMPPlaylist _playList = Player.playlistCollection.newPlaylist("myPlayList"); //build the playlist object
if (soundboardTabControl.SelectedIndex == 0)
{
fileLocation = _workingDirectory + "\\" + list.SelectedItem.ToString() + ".wav";
if (!File.Exists(fileLocation))
{
fileLocation = _workingDirectory + "\\" + list.SelectedItem.ToString() + ".mp3";
}
}
else
{
fileLocation = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + list.SelectedItem.ToString() + ".wav";
if (!File.Exists(fileLocation))
{
fileLocation = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + list.SelectedItem.ToString() + ".mp3";
}
}
if (File.Exists(fileLocation))//check for the file's existance
{
PlaySoundFile(fileLocation, list.SelectedItem.ToString()); //if the file exists then play it
}
else
{
MessageBox.Show("The file '" + list.SelectedItem.ToString() + "' couldn't be found.", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void PlayRandomSounds()
{
}
private void btnRandom_Click(object sender, EventArgs e)
{
int iterations = (int)randomIterationNumericUpDown.Value;
listBoxEntity = (ListBox)soundboardTabControl.SelectedTab.Controls[0];
if (listBoxEntity.Enabled == false)
{
//the ListBox is disabled and therefore contains no sounds, so return
return;
}
//disable the Random Button and the NumericUpDown box
btnRandom.Enabled = false;
randomIterationNumericUpDown.Enabled = false;
loopSelectedSoundCheckBox.Enabled = false;
if (mainMenuPlayAll.Checked == true)
{
playRandomSoundBothColumns(iterations);
if (soundboardTabControl.SelectedTab.Controls[0].Enabled == true)
{
btnRandom.Enabled = true;
}
randomIterationNumericUpDown.Enabled = true;
loopSelectedSoundCheckBox.Enabled = true;
return;
}
for (int i = 0; i < iterations; i++)
{
string fileLocation;
if (soundboardTabControl.SelectedIndex == 0)
{
listBoxEntity.SelectedIndex = randomNum.Next(listBoxEntity.Items.Count);
if (File.Exists(_workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav"))
{
fileLocation = _workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav";
}
else if (File.Exists(_workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3"))
{
fileLocation = _workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3";
}
else
{
MessageBox.Show("The file '" + listBoxEntity.SelectedItem.ToString() + "' couldn't be found.", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (soundboardTabControl.SelectedTab.Controls[0].Enabled == true)
{
btnRandom.Enabled = true;
}
randomIterationNumericUpDown.Enabled = true;
loopSelectedSoundCheckBox.Enabled = true;
return;
}
}
else
{
listBoxEntity.SelectedIndex = randomNum.Next(listBoxEntity.Items.Count);
if (File.Exists(_workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav"))
{
fileLocation = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav";
}
else if (File.Exists(_workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3"))
{
fileLocation = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3";
}
else
{
MessageBox.Show("The file '" + listBoxEntity.SelectedItem.ToString() + "' couldn't be found.", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (soundboardTabControl.SelectedTab.Controls[0].Enabled == true)
{
btnRandom.Enabled = true;
}
randomIterationNumericUpDown.Enabled = true;
loopSelectedSoundCheckBox.Enabled = true;
return;
}
}
//play the sound file
PlaySoundFile(fileLocation, listBoxEntity.SelectedItem.ToString(), false);
}
//renable the controls
if (soundboardTabControl.SelectedTab.Controls[0].Enabled == true)
{
btnRandom.Enabled = true;
}
randomIterationNumericUpDown.Enabled = true;
loopSelectedSoundCheckBox.Enabled = true;
}
private void PlaySoundFile(string fileLocation, string fileName, bool willLoop = true)
{
Player.URL = fileLocation;
try
{
if (Player.settings.getMode("loop"))
{
if (!willLoop)
{
Player.settings.setMode("loop", false);
Player.controls.play();
}
else
{
Player.controls.play();
}
Player.settings.setMode("loop", true);
}
else
{
Player.controls.play();
}
}
catch (InvalidOperationException Ex)
{
MessageBox.Show("The file '" + fileName + "' couldn't be played.", "Invalid Sound File", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
private void loopSelectedSoundCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (loopSelectedSoundCheckBox.Checked == true)
{
Player.settings.setMode("loop", true);
}
else
{
Player.settings.setMode("loop", false);
Player.close();
}
}
private void playRandomSoundBothColumns(int iterations)
{
int tempRandomNumber;
string fileLocation;
listBoxEntity = (ListBox)soundboardTabControl.SelectedTab.Controls[0];
for (int i = 0; i <= iterations; i++)
{
tempRandomNumber = randomNum.Next(0, soundboardTabControl.TabCount);
if (soundboardTabControl.TabPages[tempRandomNumber].Controls[0].Enabled == true)
{
soundboardTabControl.SelectedIndex = tempRandomNumber;
if (soundboardTabControl.SelectedIndex == 0)
{
if (File.Exists(_workingDirectory + "\\" + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav"))
{
fileLocation = _workingDirectory + "\\" + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav";
}
else if (File.Exists(_workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3"))
{
fileLocation = _workingDirectory + "\\" + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3";
}
else
{
return;
}
}
else
{
soundboardTabControl.SelectedIndex = tempRandomNumber;
listBoxEntity.SelectedIndex = randomNum.Next(listBoxEntity.Items.Count);
if (File.Exists(_workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav"))
{
fileLocation = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav";
}
else if (File.Exists(_workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3"))
{
fileLocation = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3";
}
else
{
return;
}
}
PlaySoundFile(fileLocation, soundboardTabControl.SelectedTab.Text, false);
}
else
{
playRandomSoundBothColumns(iterations);
}
}
}
private void mainMenuFileShowOnDisk_Click(object sender, EventArgs e)
{
listBoxEntity = (ListBox)soundboardTabControl.SelectedTab.Controls[0];
if ((String)listBoxEntity.SelectedItem == null)
{
MessageBox.Show("No sound file selected...", soundboardTabControl.SelectedTab.Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (soundboardTabControl.SelectedIndex == 0)
{
if (File.Exists(_workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav"))
{
Process.Start("explorer.exe", "/select, " + '"' + _workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav" + '"');
}
else
{
Process.Start("explorer.exe", "/select, " + '"' + _workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3" + '"');
}
}
else
{
if (File.Exists(_workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav"))
{
Process.Start("explorer.exe", "/select, " + '"' + _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav" + '"');
}
else
{
Process.Start("explorer.exe", "/select, " + '"' + _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3" + '"');
}
}
}
private void mainMenuHelpAbout_Click(object sender, EventArgs e)
{
aboutForm aboutDialogForm = new aboutForm();
aboutDialogForm.ShowDialog();
aboutDialogForm.Dispose();
}
private void mainMenuFileExit_Click(object sender, EventArgs e)
{
Close();
}
}
}