- Home /
Play a list of tracks in random order
Hi
I want to play a list of music tracks in random order right from the start of the game. Here is what I get so far....
var music : AudioClip [];
function Start ()
{
if(!audio.playOnAwake)
{
audio.clip = music[Random.Range(0,music.length)];
audio.Play();
audio.loop = true;
}
}
So it does pick a random track to play, but when that track is finished, it doesn't play the next random one, it just repeat that track.
Help me please :)
Thanks!
How/when does "music" array get populated? (Edit: Just drag/drop tracks into the exposed array via Unity editor)
Answer by vividhelix · Oct 31, 2013 at 05:46 AM
You can do something like this, once the song finishes it will play another one at random:
void PlayNextSong(){
audio.clip = music[Random.Range(0,music.length)];
audio.Play();
Invoke("PlayNextSong", audio.clip.length);
}
Answer by davem250 · Mar 23, 2014 at 11:09 PM
This is what i've learned from this page, it is in c# btw :
using UnityEngine;
using System.Collections;
public class RandomAudio : MonoBehaviour
{
public AudioClip[] soundtrack;
// Use this for initialization
void Start ()
{
if (!audio.playOnAwake)
{
audio.clip = soundtrack[Random.Range(0, soundtrack.Length)];
audio.Play();
}
}
// Update is called once per frame
void Update ()
{
if (!audio.isPlaying)
{
audio.clip = soundtrack[Random.Range(0, soundtrack.Length)];
audio.Play();
}
}
}
it is very simple yet it works it loops between the different numbers i have just tried it out and it works fine! use if you like :D
dude thx.. ppl just be careful about your script name and class name must match EXACTLY, including capital letters, spaces, etc.
Answer by renanss · Oct 31, 2013 at 05:09 AM
//This is my version of Audio Playlist.
using UnityEngine;
using System.Collections;
public class MusicBg : MonoBehaviour {
private static MusicBg instance = null;
public AudioClip[] musicbg;
private int i;
public static MusicBg Instance
{
get { return instance; }
}
void Awake() {
if (instance != null && instance != this) {
Destroy(this.gameObject);
return;
} else {
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
void Start()
{
i= Random.Range(0,musicbg.Length);
StartCoroutine("Playlist");
}
IEnumerator Playlist()
{
while(true)
{
yield return new WaitForSeconds(1.0f);
if(!audio.isPlaying)
{
if(i != (musicbg.Length -1))
{
i++;
audio.clip = musicbg[i];
audio.Play();
}
else
{
i=0;
audio.clip= musicbg[i];
audio.Play();
}
}
}
}
}
DId you apply the script to the main camera? or was it to just an audio source in the map itself?
If I walk it stops playing of that song, any ideas how to fix?
Answer by unluckyBastard · Aug 08, 2011 at 08:50 AM
it's because it's never picking a new value for random range, it's only called once in start
Try writing something in Update(). Clue: don't loop, check if still playing before playing a new one.
var music : AudioClip [];
function Update(){ if(!audio.playOnAwake){ audio.clip = music[Random.Range(0,music.length)]; audio.loop = true;
if(!audio.isPlaying){ audio.Play(); } } }
try that ^ didn't test it but I'm pretty sure it should work
I don't think that the Update will work. Just tried it and the sound can not be played, just some weird sound. I think this is because Update is called every second?
but the script checks if audio is playing before it plays a new one...I guess you can put a yield statement using the clips length
Answer by ArinFaraj · Mar 30, 2014 at 08:35 PM
var Musics : AudioClip[];
var currentM = 0;
var mMusic : AudioClip;
var mNextMusic : AudioClip;
var MusicP : AudioSource;
//var MusicTime = 100;
function Start () {
if (currentM >= Musics.length) currentM = Musics.length-1;
/*for (var i=0; i<Musics.length; i++)
DisableMusic(Musics[i]);
*/
SelectMusic(Musics[currentM]);
}
function Update () {
var IN = Input.GetButtonDown("Music");
if(IN )//||MusicP.time>MusicTime)
{
SwitchMusic(1);
}
MusicP.clip = mMusic;
if(this.ignoreListenerVolume){}
}
function SwitchMusic (iDir : int)
{
if (Musics.length < 2) return;
currentM += Mathf.Sign(iDir);
if (currentM < 0) currentM = Musics.length-1;
else if (currentM >= Musics.length) currentM = 0;
SelectMusic(Musics[currentM]);
MusicP.Play();
}
function SelectMusic(Music : AudioClip)
{
if (mMusic)
{
mNextMusic = mMusic;
}
mMusic = Music;
//MusicP.Play();
}
Your answer
Follow this Question
Related Questions
play audio from frequency and amplitude file 0 Answers
Audio clip.GetData not working 0 Answers
Make the audio manager select from an array of sounds 1 Answer
Getting audio to play on collision 3 Answers