p5.js-sound icon indicating copy to clipboard operation
p5.js-sound copied to clipboard

In a restart mode, SoudFile.isPlaying() reports wrong state while playing a sound.

Open touyou opened this issue 7 years ago • 2 comments

I found a restart mode bug. If we use restart mode, isPlaying() function show a strange behavior.

This is a sample code.

var sound;
function preload() {
    sound = loadSound('sample.mp3');
}
function setup() {
    createCanvas(windowWidth, windowHeight);
    sound.playMode('restart');
    fill(0);
    textSize(50);
    textAlign(CENTER);
}
function draw() {
    background(255);
    if (sound.isPlaying()) {
        text('playing', width / 2, height / 2);
    } else {
        text('pause', width / 2, height / 2);
    }
}
function mouseClicked() {
    sound.play();
}

If we execute this code, we can play a sound at original time every time we click the window. I think this behavior is expected, but isPlaying() function returns false every time we click twice in spite of playing state. So we can see "pause" on the screen while playing a sound.

touyou avatar Apr 20 '18 07:04 touyou

Yup, that looks like a bug to me. If you need a temporary fix (i.e., a veeeerrry dirty, yet reliable fix) in the meantime, you can hard-set sound._playing to "true" a millisecond after you run sound.play();. Try this:

function mouseClicked() {
   sound.play();
   setTimeout(() => {sound._playing = true}, 1);
}

datramt avatar May 02 '18 04:05 datramt

@touyou @montoyamoraga @therewasaguy

after little experiments and deep dive into the matter , i found that everything is working fine if i use local build of p5.sound.js ( latest ofcourse )
but the problem was there while using cdn link for p5.sound.js file

here is the proof of my experiment 1.using local build of p5.sound file works perfectly fine :relieved: 2.using cdn link for p5.sound file but this doesn't :grimacing:

only difference between them is that 1 uses local p5.sound.js file build by latest code of the repo & 2 uses cdn link of p5.sound.js

may be cdn links are not updated ?

endurance21 avatar May 01 '20 21:05 endurance21