private void playKeySound(XMLSettings.SoundHotkey currentKeysSounds) { Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath); string path; if (currentKeysSounds.SoundLocations.Length > 1) { //get random sound int temp; while (true) { temp = rand.Next(0, currentKeysSounds.SoundLocations.Length); if (temp != lastIndex && File.Exists(currentKeysSounds.SoundLocations[temp])) { break; } Thread.Sleep(1); } lastIndex = temp; path = currentKeysSounds.SoundLocations[lastIndex]; } else { path = currentKeysSounds.SoundLocations[0]; //get first sound } if (File.Exists(path)) { float customSoundVolume = currentKeysSounds.SoundVolume; //use custom sound volume if the user changed it if (customSoundVolume < 1) { playSound(path, customSoundVolume); } else { playSound(path, soundVolume); } keysJustPressed = currentKeysSounds.Keys; } else if (!showingMsgBox) //dont run when already showing messagebox (don't want a bunch of these on your screen, do you?) { SystemSounds.Beep.Play(); showingMsgBox = true; MessageBox.Show("File " + path + " does not exist"); showingMsgBox = false; } }
private void btnCreateWAVAdd_Click(object sender, EventArgs e) { if (!string.IsNullOrWhiteSpace(tbText.Text) && !string.IsNullOrWhiteSpace(tbKeys.Text) && !string.IsNullOrWhiteSpace(tbWhereSave.Text) && Directory.Exists(tbWhereSave.Text)) { Keys[] convertedKeys; string error; if (Helper.keysArrayFromString(tbKeys.Text, out convertedKeys, out error)) { if (convertedKeys.Length > 0) { var newSH = new XMLSettings.SoundHotkey(convertedKeys, "", new string[] { tbWhereSave.Text + "\\" + Helper.cleanFileName(tbText.Text.Replace(" ", "") + ".wav") }); synth = new SpeechSynthesizer(); synth.SetOutputToWaveFile(newSH.SoundLocations[0]); PromptBuilder builder = new PromptBuilder(); builder.AppendText(tbText.Text); synth.Speak(builder); synth.Dispose(); synth = null; mainForm.soundHotkeys.Add(newSH); var newItem = new ListViewItem(tbKeys.Text); newItem.SubItems.Add(""); //window title newItem.SubItems.Add(newSH.SoundLocations[0]); mainForm.lvKeySounds.Items.Add(newItem); mainForm.lvKeySounds.ListViewItemSorter = new ListViewItemComparer(0); mainForm.lvKeySounds.Sort(); mainForm.soundHotkeys.Sort(delegate(XMLSettings.SoundHotkey x, XMLSettings.SoundHotkey y) { if (x.Keys == null && y.Keys == null) { return(0); } else if (x.Keys == null) { return(-1); } else if (y.Keys == null) { return(1); } else { return(Helper.keysToString(x.Keys).CompareTo(Helper.keysToString(y.Keys))); } }); MessageBox.Show("File saved to " + newSH.SoundLocations[0]); } } else { MessageBox.Show("Keys string incorrectly made. Check for spelling errors"); } } else { MessageBox.Show("No text in text box, keys box, and/or where to save box... or the where to save folder does not exist"); } }