hi I have this 'void task playaudio' private async Task PlayAudio(InputSimulator ISim,…
- User
hi I have this
'void task playaudio' private async Task PlayAudio(InputSimulator ISim, TimeSpan duration, bool bypass) { try { if (RealisticVolume) { Random random = new Random();
// Generate a random float between 0.7 and 1.0 float volume = 0.7f + (float)random.NextDouble() * (1.0f - 0.7f); // Output the random volume vol = volume; } if (ChatCombo == true && bypass == false) { string command = PartyMode ? "/party" : "/team"; ISim.Keyboard.KeyPress(VirtualKeyCode.RETURN); await Task.Delay(40); ISim.Keyboard.TextEntry(command); await Task.Delay(40); ISim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V); await Task.Delay(30); ISim.Keyboard.KeyPress(VirtualKeyCode.RETURN); toolStripStatusLabel1.Text = $"TTS: {Quote}{GlobalOutput}{Quote}"; } // Initialize a new WaveOutEvent and AudioFileReader _waveOut = new WaveOutEvent { DeviceNumber = OutDev, Volume = vol }; _audioFileReader = new AudioFileReader(audioFilePath); _waveOut.Init(_audioFileReader); _waveOut.Play(); if (!bypass) { // Determine the key to press based on PartyMode keyToPress = PartyMode ? VirtualKeyCode.VK_0 : VirtualKeyCode.VK_9; } if (P2T == true && bypass == false) { ISim.Keyboard.KeyDown(keyToPress); } if (!bypass) { // Wait for the duration of the wave file plus extra time await Task.Delay((int)duration.TotalMilliseconds + 1000); } if (P2T == true && bypass == false) { ISim.Keyboard.KeyUp(keyToPress); } // Wait until playback is complete and clean up resources while (_waveOut.PlaybackState == PlaybackState.Playing) { await Task.Delay(100); } // Clean up existing audio resources _audioFileReader?.Dispose(); _waveOut?.Stop(); _waveOut?.Dispose(); //File.Move(audioFilePath, @"C:\Users\DjZaN\OneDrive\Desktop\Digital_Breath\Content\Result\Result.wav"); if (!P2T && ConfMode) { PlayWavFile(@"C:\Users\DjZaN\OneDrive\Desktop\Digital_Breath\Content\SFX\Ready.wav"); } } catch (Exception ex) { // It's a good practice to log the exception or handle it accordingly Console.WriteLine($"An error occurred: {ex.Message}"); // Optionally log ex.StackTrace or other info for debugging } if (RealisticVolume) { volumeSlider1.Volume = vol; }}
and I want to be able to stop the task then start it each time its started to prevent spam.
- Assistant
- User
provide complete script
- Assistant
- User
is there not a very simple way to cancel the task
- Assistant