I'm using the p5.sound library and can't get a song to play again after I've paused it

My index.html file:

The sketch.js file:

var song;

function setup() {
createCanvas(600,400);
song = loadSound(“suprematism.mp3”);
}

function draw() {
background(0);
}

I have just been using the console to play it and pause it. If I type song.play(); it works fine. And if I type song.pause(); it pauses. But when I try to do song.play() again it won’t start. Although the first time I try to play it again, I do notice a short pop. But on subsequent tries, nothing.

I’m trying to copy Daniel Shiffman’s tutorial.

I’m a bit confused about referencing the p5.sound library in your html file. Question, what’s the difference between p5.sound.js and p5.sound.min? Should I have both referenced? A specific one? Or one or the other?

I also noticed that my html has a bit different wording than his in the video. I assume that’s because the tutorial is old and the example folder that you get when downloading p5 has changed since then. Since all of the sound library functions were working up until now, I just assume it has the sound library referenced right out of the box.

Any ideas?

Try to put

soundFormats('mp3');
song = loadSound('supermatism.mp3');

in your preload function.

1 Like

Does this code work for you?

var soundFile;

function preload() {
  soundFormats('ogg', 'mp3');
  soundFile = loadSound('supermatism.mp3');
}

function setup() {
  background(0, 255, 0);
  soundFile.setVolume(0.1);
  soundFile.play();
}

function keyTyped() {
  if (key == 'p') {
    soundFile.pause();
    background(255, 0, 0);
  }
  if (key == 's') {
    soundFile.play();
    background(0, 255, 0);
  }
}
1 Like

Hello! Thank you for replying!

I added the bit about formats to my preload function. That alone didn’t work. So I copied the example you gave and wasn’t able to get that to work either.

Everything is working perfectly except for once I pause, I can’t get the song to play again.

I even used the rate() and pan() functions yesterday and they worked great. That should indicate that the issue isn’t with how I have referenced the p5.sound library, right?

Also. In the console I checked the isPlaying() function and that shows true and false as you would expect.

It shows true when I first start playing it. And when I pause it, it shows false. But then it goes back to true again once I play it, but there’s nothing playing as far as I can tell.

I just downloaded Firefox and found that it works in that. I was using Chrome before.

This makes me sad.