Working in p5.js on a class assignment. We have to make a music player. I found a tutorial in the site, but despite copying the code near-exactly (only altering so as to implement multiple songs), I can't seem to get the play button to work. What's stopping it from reading my files?
If you want to test it for yourself, I got the music files from the incompetech library.
var track = 1;
let song1, song2, song3, song4, song5;
function setup() {
createCanvas(500, 250);
song1 = loadSound('Burn The World Waltz.mp3');
song2 = loadSound('Le Grande Chase.mp3');
song3 = loadSound('Mesmerizing Galaxy.mp3');
song4 = loadSound('Guzheng City.mp3');
song5 = loadSound('Holiday Weasel.mp3');
//setup play
let playButton;
playButton = createButton('>||');
playButton.size(100, 100);
playButton.position(200,150);
playButton.style('background:green');
playButton.mousePressed(toggleSong);
//setup shift_track_right
let rightButton;
rightButton = createButton('>>|');
rightButton.size(100, 100);
rightButton.position(300,150);
rightButton.style('background:cyan');
rightButton.mousePressed(right);
//setup shift_track_left
let leftButton;
leftButton = createButton('|<<');
leftButton.size(100, 100);
leftButton.position(100,150);
leftButton.style('background:cyan');
leftButton.mousePressed(left);
}
function left()
{
if (track <= 5)
{
track -= 1;
}
}
function right()
{
if (track >= 1)
{
track += 1;
}
}
function toggleSong()
{
if (track === 1)
{
if (song1.isPlaying())
{
song1.stop();
}
else {
song1.play();
}
}
else if (track === 2)
{
if (song2.isPlaying())
{
song2.stop();
}
else {
song2.play();
}
}
else if (track === 3)
{
if (song3.isPlaying())
{
song3.stop();
}
else {
song3.play();
}
}
else if (track === 4)
{
if (song4.isPlaying())
{
song4.stop();
}
else {
song4.play();
}
}
else if (track === 5)
{
if (song5.isPlaying())
{
song5.stop();
}
else {
song5.play();
}
}
}
function draw() {
background(220);
if (track === 1)
{
title = 'Burn the World Waltz';
}
else if (track === 2)
{
title = 'Le Grand Chase';
}
else if (track === 3)
{
title = 'Mesmerizing Galaxy';
}
else if (track === 4)
{
title = 'Guzheng City';
}
else if (track === 5)
{
title = 'Holiday Weasel';
}
textSize(25)
text(title, 100, 100)
}
This won't fix all your problems, but I found discrepancies in the first three filenames compared to the names of the files downloaded from the site you mentioned. These will cause load or download errors when your code is run.
'Burn The World Waltz.mp3' // Rename the downloaded file to remove the space before .mp3
'Le Grand Chase.mp3' // Removed the e from end of Grand to match download
'Mesmerizing Galaxy Loop.mp3' // Appended " Loop" to match download