- Home /
How to play several audio cllips one after another.
Hi, heres what i'm trying to do this script I have here is great, but it only plays one audio clip(Clip1) after the first one, I need it to play ten one after another, and then once it gets through them all restarts and goes again,thanks.
var Clip1: AudioClip; var playNow = false;
yield WaitForSeconds(audio.clip.length); playNow = true;
function Update () {
if(playNow) { // Assign Clip1 and play it audio.clip = Clip1; audio.Play(); playNow = false; } }
Answer by Olgo · Nov 20, 2012 at 06:34 PM
Create an array of audio clips, create a for loop inside your if(playNow) statement. At the of the for loop, yield for the length of the audio clip, then increment the counter to go to the next item in the array.
Thanks, but I am new to code and I would be able to do that, sorry if there's anyway you can write the code for me that would be great, so sorry again.
I'm pretty new myself, but I like to use UnityAnswers for learning exercises. That said, I think I was able to put something together that will work for you. I'll post it in a new answer so we can have the nice formatting.
Answer by Olgo · Nov 20, 2012 at 09:19 PM
Paste this into PlaySoundArray.cs, add it to a game object with an audio source. Drag the sound clips into the array once you have specified the length.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (AudioSource))]
public class PlaySoundArray : MonoBehaviour {
public float hSliderValue = 1.0f;
public AudioClip[] clips;
public int hideGUItime = 5;
private bool GUIhidden = false;
private float mouseMoveTime = 0f;
private bool playNow = false;
private int cnt = 0;
private Vector3 tmpMousePos;
void Update () {
DetectMouseMov();
mouseMoveTime = mouseMoveTime + Time.deltaTime;
if(mouseMoveTime > hideGUItime)
GUIhidden = true;
else
GUIhidden = false;
if(Input.GetKeyUp(KeyCode.M)){
playNow = !playNow;
audio.Stop();
}
if(playNow){
PlaySounds();
}
}
void PlaySounds(){
if(!audio.isPlaying && cnt < clips.Length){
audio.clip = clips[cnt];
audio.volume = hSliderValue;
audio.Play();
cnt = cnt + 1;
}
if(cnt == clips.Length)
cnt = 0;
}
void OnGUI() {
if(!GUIhidden){
GUI.Label(new Rect(25, Screen.height - 50, 100, 30), "Volume");
hSliderValue = GUI.HorizontalSlider(new Rect(25, Screen.height - 25, 100, 30), hSliderValue, 0.0f, 1.0f);
}
Screen.showCursor = !GUIhidden;
}
void DetectMouseMov(){
if (tmpMousePos != Input.mousePosition){
mouseMoveTime = 0;
tmpMousePos = Input.mousePosition;
}
}
}
Thanks Olgo, but when I assign it to my main camera, and add two or more audio clips just like you said, nothing happens and there's no errors or anything either.
Blast! I wasn't able to test it when I wrote it because I was at work. Still am, but I'm being sneakier. Looks like we never make it into the IEnumerator PlaySounds function. However, I change IEnumerator to void and it plays just fine, but we don't get to use the nifty yield command to wait for the end of the sound. Gimme a couple $$anonymous$$utes and I'll have it fixed. I have an idea.
Just to keep things cleaner around here. I got rid of the old script and updated this original answer to contain the working one.
This will cycle through your array of sounds as long as playNow is true. It will stop when playNow is false, and pick up where it left off when playNow is true again.
Thanks Olgo it works great, now if you could make it so when the player clicks the "m" key on the keyboard then it plays the audio clips and if the click it again it pauses the audio clips, ins$$anonymous$$d of it just doing it on start, I could probably do it, but I only know Java script and Im not good at converting code, if you don't, have time thats fine Thanks again Olgo.
No problem, I'll update the answer again with the latest changes. I've tested it, the $$anonymous$$ key starts and stops the cycle now. We have version 3.0 :) maybe more like 2.1
Answer by Taimoor_Ahmed · Aug 11, 2017 at 02:13 PM
Hey @Jammer3000; Solution to your problem is;
// declare Variable
private bool OnPauseMenuSong = false;
void Update(){
if (playNow)
{
PlaySounds();
OnPauseMenuSong = !OnPauseMenuSong;
if(OnPauseMenuSong == true){
PlaySound();
}
}