Stopping Audio from playing when another is active and holding key for Audio

Hi there;

I’m trying to script audio for a dog in my game so that when the key is pressed, the audio plays.

This part I’ve got sorted, but what I wanted to do was,
A = Make it so that the other audio can’t be activated when the other is playing, like, you can’t howl if you’re barking. So I guess, making it so that the others can’t be pressed till a certain duration.

I started working on this but was hoping for a more efficient way of doing it. Here is the relevant code I was working on:

{

// Whine
public AudioSource whineAudio;
public bool canWhine;
public float whineTime;
public bool isWhining;

// Bark
public AudioSource barkAudio;
public bool canBark;
public float barkTime;
public bool isBarking;

// Howl
public AudioSource howlAudio;
public bool canHowl;
public float howlTime;
public bool isHowling;

void Update()
{
    Audio();
}

void Audio()
{
    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
        isWhining = true;
        whineAudio.Play();
    }
    else if (Input.GetKeyUp(KeyCode.Alpha1))
    {
        isWhining = false;
    }
    if (Input.GetKeyDown(KeyCode.Alpha2))
    {
        isBarking = true;
        barkAudio.Play();
    }
    else if (Input.GetKeyUp(KeyCode.Alpha2))
    {
        isBarking = false;
    }
    if (Input.GetKeyDown(KeyCode.Alpha3))
    {
        isHowling = true;
        howlAudio.Play();
    }
    else if (Input.GetKeyUp(KeyCode.Alpha3))
    {
        isHowling = false;
    }
}

The second thing I wanted to do was make it so that the button for howling had to be held for a certain duration before the Audio played

Sorry for the incomplete code for the task but was really hoping to find a better way of doing it. Thank you very much!

Make sure you import the System.Collections for using IEnumerators

 // Whine
 public AudioSource whineAudio;
 // Bark
 public AudioSource barkAudio;
 // Howl
 public AudioSource howlAudio;

//Check if can bark or Howl or Whine
private bool canPlayAudio = true;

 void Update()
 {
     Audio();
 }
 void Audio()
 {
     if (Input.GetKeyDown(KeyCode.Alpha1) && canPlayAudio == true)
     {
         StartCoroutine(DelayAudio(1f, whineAudio));      
     }
     
     if (Input.GetKeyDown(KeyCode.Alpha2) && canPlayAudio == true)
     {      
         StartCoroutine(DelayAudio(1f, barkAudio));
     }
  
     if (Input.GetKeyDown(KeyCode.Alpha3) && canPlayAudio == true)
         {
            StartCoroutine(DelayAudio(1f, howlAudio));
         }
    
     IEnumerator DelayAudio(float delaySec, AudioSource audio)
     {
         canPlayAudio = false;
        //wait for the seconds specified
         yield return new WaitForSeconds(delaySec);
         audio.Play();
         
         //Wait until the audio is finished
         yield return new WaitUntil(() => audio.isPlaying == false);
         canPlayAudio = true;
     }
 }