Timble
Timble

Reputation: 499

How To Pick Random Sound From Array? (JS)

Looked but could not find an answer to my specific problem.

What I want to do:

Call this function from my HTML or in another JS function and play a random sound selected from an array in my JS.

Playing just one sound using slightly different code works

I can just play one sound easily enough, my code works in that sense, but I get this error in the console when trying to play multiple sounds with the below code:

audio.js:90 Uncaught TypeError: Cannot read property 'call' of undefined

My code doesn't work, how to fix it?

Here is my code so far:

var SoundRandom = function (soundNameRandom) {
this.soundNameRandom = soundNameRandom;
}

SoundRandom.prototype.playRandomSound = function() {
RandomSound = new Audio(this.soundNameRandom);
var randomSound = Math.random() * 10;
if (randomSound > 5 && isMuted == false) {
  RandomSound[0].play();
 }
 else if (randomSound < 6 && isMuted == false) {
  RandomSound[1].play();
 }
}

var randomSound = new SoundRandom(["sword.mp3", "openinvy.mp3"]);

I called it using

randomSound.playRandomSound();

My Question

What is wrong with my code? What did I do wrong or what should I change? Is what I want to do possible with just Javascript? I'd rather not use Jquery. I'm relatively new to the audio side of Javascript so there may be something I'm unaware of.

Also, I don't want to use the HTML audio tag, I want to keep everything in JS, which as I stated earlier DOES work for single sound clips but not a random sound like I'm trying to do.

Upvotes: 1

Views: 919

Answers (2)

Kevin Wang
Kevin Wang

Reputation: 281

Try this:

SoundRandom.prototype.playRandomSound = function() {
var randomSound = Math.random() * 10;
if (randomSound > 5 && isMuted == false) {
  RandomSound = new Audio(this.soundNameRandom[0]);
  RandomSound.play();
 }
 else if (randomSound < 6 && isMuted == false) {
  RandomSound = new Audio(this.soundNameRandom[1]);
  RandomSound.play();
 }
}

I do not know if this will work. However, a Audio element must be a string, but instead you are putting in an array in the new Audio(). The goal of my code was to first get the random integer, then making a new audio based on the names given in the array and play that element.

Upvotes: 2

Rob M.
Rob M.

Reputation: 36511

There are a few errors here: RandomSound is not an array, it is an Audio object. I think you are wanting to pick a random song from whatever is passed to SoundRandom. To do this, you need to generate a random number between 0 and the length of the array passed to the SoundRandom constructor, create the Audio object with the file at your random index. This would look more like this (haven't tested it):

var SoundRandom = function (sounds) {
  this.sounds = sounds;
}

SoundRandom.prototype.playRandomSound = function() {
  var randomIndex = Math.floor(Math.random() * this.sounds.length);
  var randomSound = new Audio(this.sounds[randomIndex]);

  // play random sound
  if (isMuted === false) {
    randomSound.play();
  }
}

var soundRandom = new SoundRandom(["sword.mp3", "openinvy.mp3"]);
soundRandom.playRandomSound();

Upvotes: 2

Related Questions