简体   繁体   English

在鼠标点击时从预加载播放随机声音文件(mp3 文件)

[英]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. Math.random()生成一个介于 0.0 和 1.0 之间的随机数。 So Math.random()*sounds.length is a floating point number >= 0.0 and < sounds.length .所以Math.random()*sounds.length是一个浮点数 >= 0.0 和 < sounds.length Math.floor gets the integral value which is less or equal the number. Math.floor获取小于或等于数字的整数值。

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.为了确保一次只播放一个声音,您必须在变量 ( currentSound ) 中记下当前声音并验证声音是否已完成播放,然后才能开始新声音。
Furthermore use the mousePressed() callback rather than the built-in state variable mouseIsPressed .此外,使用mousePressed()回调而不是内置的 state 变量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);


}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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