this task plays audio and holds the push to talk key for the duration of the audioclip…
- User
this task plays audio and holds the push to talk key for the duration of the audioclip its playing but when I play another clip its not adding to the wait time to hold the push to talk button down thus resulting in the push to talk key being lifted too early and so on.
private async Task PlayAudio(InputSimulator ISim, TimeSpan duration, bool bypass) {
// If an audio task is already running, cancel it if (_isPlaying) { _cancellationTokenSource.Cancel(); _cancellationTokenSource.Dispose(); _cancellationTokenSource = new CancellationTokenSource(); } // Set the flag to indicate that the audio is currently playing _isPlaying = true; try { // Get the cancellation token from the cancellation token source CancellationToken token = _cancellationTokenSource.Token; // Check if the audio is already playing and stop it if so if (_waveOut != null && _waveOut.PlaybackState == PlaybackState.Playing) { _waveOut.Stop(); _audioFileReader?.Dispose(); _audioFileReader = null; // Set it to null to avoid further use _waveOut.Dispose(); _waveOut = null; // Set it to null to avoid further use } 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, token); } if (P2T == true && bypass == false) { ISim.Keyboard.KeyUp(keyToPress); } // Wait until playback is complete and clean up resources while (_waveOut.PlaybackState == PlaybackState.Playing && !token.IsCancellationRequested) { await Task.Delay(100, token); } // Clean up existing audio resources if not cancelled if (!token.IsCancellationRequested) { _audioFileReader?.Dispose(); _waveOut?.Stop(); _waveOut?.Dispose(); } if (!P2T && ConfMode && !token.IsCancellationRequested) { PlayWavFile(@"C:\Users\DjZaN\OneDrive\Desktop\Digital_Breath\Content\SFX\Ready.wav"); } } catch (TaskCanceledException) { // Handle the task cancellation appropriately Console.WriteLine("Playback was cancelled."); } catch (Exception ex) { // Handle other exceptions Console.WriteLine($"An error occurred: {ex.Message}"); } finally { _isPlaying = false; // Reset the flag if (RealisticVolume) { volumeSlider1.Volume = vol; } } - Assistant