简体   繁体   中英

Play random sound file (mp3 file) from preload on mouse click

"I want it so when mouseispressed it plays a random sound file from the three soundfiles preloaded already at the top of the code. at the moment i can only play one sound file at a time "

function preload() {
    bird = loadSound('kooka.mp3');
    bird2 = loadSound('galah.mp3');
    bird3 = loadSound('cuckoo.mp3');
}

function setup() {
    createCanvas(windowWidth, windowHeight);
    background(0);
}

function draw() {
    kooka(); 
}

function kooka () {

    if (mouseIsPressed) {

        bird2.playMode('untildone');
        bird2.play();
        bird2.setVolume(0.3);

Create an array of sounds and "select" a random sound from the array:

let sounds = [bird, bird2, bird3];
let randomSound = sounds[Math.floor(Math.random()*sounds.length)];

Math.random() generates a random number between 0.0 and 1.0. So Math.random()*sounds.length is a floating point number >= 0.0 and < sounds.length . Math.floor gets the integral value which is less or equal the number.

IF the mouse button is pressed multiple times, multiple sounds would be played. To ensure that just one sound plays at once you've to note the current sound in variable ( currentSound ) and to verify if the sound has finished playing, before you can start a new sound.
Furthermore use the mousePressed() callback rather than the built-in state variable mouseIsPressed . The event occurs only one when the mouse is pressed, while the variable is stated as long the mouse is pressed. eg:

function draw() {
}

let currentSound;
function mousePressed() {

    let is_playing = currentSound && currentSound.isPlaying();
    if (!is_playing) {

        let sounds = [bird, bird2, bird3];
        currentSound = sounds[Math.floor(Math.random()*sounds.length)];

        currentSound.playMode('untilDone');
        currentSound.play();
        currentSound.setVolume(0.3);
    }
}
var input;
var mic; 
var bird;
var bird2;
var bird3; 
var bird4; 
var bird5; 
var bird6; 


function preload() {
       bird = loadSound('kooka.mp3');
       bird2 = loadSound('galah.mp3');
       bird3 = loadSound('lyre.mp3');
       bird4 = loadSound('friar.mp3');
       bird5 = loadSound('whistler.mp3');
       bird6 = loadSound('cuckoo.mp3');

    }


function setup() {
  createCanvas(windowWidth, windowHeight);
  background(0);
  input = new p5.AudioIn();
  input.start();


}

function draw() {



     var volume = input.getLevel();
     var threshold = 0.2;
     var regularArr = [bird, bird2, bird3, bird4, bird5, bird6];

    if (volume > threshold) {
    shuffle(regularArr, true);
        var newArr = shuffle(regularArr);
        var beh = newArr[0]; 
        var bef = newArr[1]; 
        var ben = newArr[2];
        var beq = newArr[3];
        var bek = newArr[4];
        var bez = newArr[5];

        beh.playMode('untilDone');
        beh.play(0.1);
        bef.stop(); 
        ben.stop();
        beq.stop(); 
        bek.stop(); 
        bez.stop();  
        print(beh);


}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM