How to play loop an audio clip on button hold down?

Hi I have this code(code below) that is working, but I don’t just want it to play the audio once then stop. I need it so that when I hold down the left, right, up or down arrow keys it will loop the my audio clip which is about 1 to 2 seconds long, but stop when the arrow keys are not being pressed. It’s my characters walking sound by the way and the audio clip I’m trying to play is a footstep sound. Also Please do it in java. Thanks

Here’s the code:

var sound : AudioClip;

function Update ()
{
if(Input.GetButtonDown(“Fire1”))
{
audio.PlayOneShot(sound);
audio.Play();
}
}

Just add a simple check to see if the audio is already playing:

var sound : AudioClip;

function Update () { 

  if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) 
  { 
    if(!audio.isPlaying)
    {
      audio.PlayOneShot(sound); 
      audio.Play(); 
    }
  } 

}

Here’s an alternative that lets you control how much of the audio clip you want to play before looping:

var sound : AudioClip;
var timer : float = 100.0;
var clipLength : float;

function Start() {
  //clip the end of the audio file by 0.2 sec
  clipLength = sound.clip.length - 0.2;
}
    
function Update () { 

  if(audio.isPlaying)
  {
    timer += Time.deltaTime;
  }

  if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) 
  {
     if(timer >= clipLength)
     {
        audio.PlayOneShot(sound); 
        audio.Play(); 
        timer = 0.0;
     }
  } 

}

Try audio.loop(sound);

void Update ()
{
if (Input.GetKeyDown (KeyCode.UpArrow)) {

		sample.Play ();

	} else if (Input.GetKeyUp (KeyCode.UpArrow)) {
		sample.Stop ();
	}