Randomized Music Intro Code (Help)

Greetings,

I was wondering if I could have some assistance with this code. I’m using a loading screen code that allows me to use custom music and it works great, but I was wondering if I couldn’t put in a random.math code section to randomly select each song… I’m not quite sure how to formulate the code so if I could have assistance that would be great.

Here is the code:

[
	{
		"useBackgroundColour" : "true",
		"backgroundColour" : "20,20,20",
		"useBackgroundImage" : "true",
		"backgroundImagePath" : "images/backgroundImg1.jpg",
		"useTextColour" : "false",
		"textColour" : "0,0,0",
		"useBoldText" : "false",
		"playAudioInBackground" : "true",
		"audioPath" : "audio/gtavthemesong2.ogg",
		"audioVolume" : "0.5"
	},

Each additional audio song ranges from gtavthemesong.ogg to gtavthemesong15.ogg I was thinking

math.random(1, 15) 
if 1 then 
 		"audioPath" : "audio/gtavthemesong.ogg",
if 2 then 
      		"audioPath" : "audio/gtavthemesong2.ogg",

etc. and so on. I’m sure my code is awfully put together as I’m not good at writing code.

Does anyone have any ideas?

What I would do is turn the audioPath JSON from a variable into an array, which would look something like this:

"audioPath": [
    "path1",
    "path2",
    "path3"
],

and so on.

Then, inside of the javascript, I would randomize it by the length of the array but you don’t need the elseif stuff

var rnd = math.random(1, array.length);
var song = array[rnd-1];
// play the song here i guess

And that’s it, just make sure to change the variables to what they are supposed to be inside of the code so that you don’t get a crash when you startup the script.

Oh hey BlockBa5her. I use a lot of your scripts, thanks for the help! I’ll be sure to test this out.

1 Like

Would

var rnd = math.random(1, array.length);
var song = array[rnd-1];
// play the song here i guess

Go into the index.html file? I’m a little confused on the location, and I assume in the var rnd = math.random(1, array.length); array syntax I should put audioPath? I’m confused what the length would be defining or if its necessary? Each song is 2 minutes and the intent is so that when a player loads in it randomly chooses one of the 15 predefined songs, or it could randomize on server restart either works. Each player takes about 30 seconds to load in so its unlikely they’ll even listen through a full song, is that what the length is defining, the length of the song to play?

This should be put in the JavaScript file where the music playing is contained

Okay I think I found the code snippet in the material_load.js

		if(config[0].playAudioInBackground == "true")
		{
			var audio = new Audio('' + config[0].audioPath);
			audio.play();
  			audio.volume = config[0].audioVolume;
		}

So I guess it would be…

var rnd = math.random(1, audioPath.length);
var song = audioPath[rnd-1];
// play the song here i gues

		if(config[0].playAudioInBackground == "true")
		{
			var audio = new Audio('' + config[0].audioPath);
			audio.play();
  			audio.volume = config[0].audioVolume;
		}

Does that look correct?

Almost, it would probably look something like

var rnd = math.random(1, config[0].audioPath.length);
var song = config[0].audioPath[rnd-1];
// play the song here i gues

		if(config[0].playAudioInBackground == "true")
		{
			var audio = new Audio('' + song);
			audio.play();
  			audio.volume = config[0].audioVolume;
		}

Does the “song” part need to be replaced with the gtathemesong1.ogg integer or is the code good to go as it is? Would I have to repeat that code with config[1] for each different song up to 15?