Play next sound in list - On keypress

Hi all,

I’ve had to resort to asking on here because after millions of attempts I can’t get it to work.

What I want is a list of var Audioclips, when you press a key (“H”) it will play the first Var, when you press “H” again, it will play the next Audioclip var etc until you get to the last one, press “H” and it starts at the begining again. To turn the sound off you have to double tap “H”

Currently on mobile so don’t have access to what I have so far, any help will be appreciated.

Something like (the script below is in JS assuming you require it in JS. Let me know if you need it in C#):

// You can assign the audio clips from Inspector
var audioclips : AudioClip[];
var clipCounter : int = 0;
var lastKeyPressTime : float;
var maxDoubleKeyPressTime : float = 0.5f;
var currentAudio : AudioSource;

function Update()
{
    if(Input.GetKeyDown(KeyCode.H))
	{		
		if(lastKeyPressTime > maxDoubleKeyPressTime)
		{
		lastKeyPressTime = 0;
		currentAudio = AudioSource.PlayClipAtPoint(audioclips[clipCounter], new Vector3(0,0,0));
			if(clipCounter == audioclips.Length)
			{
				clipCounter = 0;
			}
		}
		else
		{
			if(currentAudio.isPlaying)
			{
				lastKeyPressTime = 0;
				currentAudio.Stop();				
			}
		}
	}
	lastKeyPressTime += Time.deltaTime;
}

This code is not tested but you can get the logic.

Hi there! I saw @HarshadK posted a reply before me, but since I’ve put some effort in this too I’ll post you my reply anyway :smiley:

It’s in C#, the only thing that’s missing is the play audio stuff… I used integers in my example because I’m a newbie and never tried to use sounds.
Nevertheless this thing down here is working and hopefully as you requested!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class soundArray : MonoBehaviour {

	/* I usually code in C# but hopefully a web translator will translate this stuff in java if you need so.
	 * 
	 * This class will handle the array/list behaviour you're asking for, just populate the list/array
	 * I will use integers to make it work in this example.
	 * 
	 * I don't know if you're confident with coding, so I'll be explicit:
	 * look that line above ( the third), it's important to use Lists, an updated kind of arrays.
	 * 
	 */

	// The list you need, change its type to match sounds
	private List<int> soundList = new List<int>();
	// an index for that list
	private int soundIndex;

	// Use this for initialization
	void Start () {

		//You want to populate the list with your sounds, I'll add some int here
		soundList.Add(123);
		soundList.Add(456);
		soundList.Add(789);

		//Check to see if your list contains those elements
		Debug.Log("Items in soundList:" +soundList.Count);
		//Should be all right

		soundIndex = 0; // let's start from the first item in list
	
	}
	
	// Update is called once per frame
	void Update () {

		// Here you want to check if H is pressed
		if(Input.GetKeyDown(KeyCode.H)){
			// Then you want to execute something, like the method you requested
			playNext();
		}
	
	}

	// The function you're requiring
	void playNext(){
		//Please, note that I'm not a professional, you'll have to discover by yourself the method to
		//play a sound, I'll just give you my two cents about how to play the next one in the list/array

		if(soundIndex < soundList.Count){
			// if this item is not the last item in the list

			// execute the method to play a sound, I'll just ask console to print the current int
			Debug.Log ("soundIndex is:"+soundList[soundIndex]);

			// then add 1 to your index to play the next sound, it won't go out of range because of the next if
			soundIndex += 1;
		}

		if(soundIndex >= soundList.Count){
			// if this item is out of range
			soundIndex = 0;
		}

	}
}

i can give you an idea for how to do that.
create an array that will hold your clips. for example:

public AudioClip list;

you can drag and drop clips into the array in the inspector.

every time you will press “H” advance the array index and play the next clip:

if(pressd “h”) {

   audio.clip = list[index+1];
    audio.Play();

}

for double tap you need to build mechanism that will recognize it. for example
when you press “H” once check for time and if second “H” is pressed then it’s double tap and you can stop the music…

hope it’s help. sorry i can’t post full code.