Hi! I’m trying to create a website with p5 that plays a video back and then also uses a slider to pan a song that is simultaneously playing. I followed example code from the p5 website, however for some reason, my button doesn’t start the video when clicked and my slider is not panning the audio. Can anyone help me figure this out?
SKETCH:
let cnv;
let video;
let videoReady = false;
let playButton;
let song;
let sliderPan;
function modelLoaded() {
console.log("Model Loaded");
videoReady = true;
}
function songLoaded() {
song.play();
}
//setup code
function setup() {
const w = 640;
const h = 480;
video = createVideo(['randomvid.mp4', 'randomvid.ogv', 'randomvid.webm']);
video.parent('video');
video.size(w, h);
playButton = document.getElementById('play-button');
playButton.addEventListener('click', playPressed);
song = loadSound('dhol_main_loop.mp3', songLoaded);
sliderPan = createSlider(-1, 1, 0, 0.01);
}
function draw() {
if (!videoReady) {
textSize(32);
fill('#222');
textAlign(CENTER, CENTER);
text('loading...', 320, 240);
return;
}
background(150);
song.pan(sliderPan.value());
}
function mousePressed() {
video.play();
}
function toggleVideo() {
if (!video.isLooping()) {
video.loop();
playButton.innerText = "Stop Video";
} else {
video.stop();
playButton.innerText = "Play Video";
}
}
function playPressed() {
toggleVideo();
}
INDEX:
<!DOCTYPE html>
<html>
<head>
<title>Sample Website</title>
<script src="ml5.min.js" type="text/javascript"></script>
<script src="p5.min.js"></script>
<script src="p5.dom.min.js"></script>
<script src="p5.sound.min.js"></script>
<link
rel="stylesheet"
type="text/css"
href="style.css" />
<link
rel="preconnect"
href="https://fonts.gstatic.com" >
<link
href="https://fonts.googleapis.com/css2?family=Nunito+Sans&display=swap"
rel="stylesheet" >
<link
rel="stylesheet"
href="style.css" />
<meta charset="utf-8" />
</head>
<body>
<header id="masthead">
<h1>Sample Website</h1>
</header>
<main id="video">
</main>
<footer id="bottom-toolbar">
<div id="audio-controls">
<button id="play-button" aria-label="Play">Play Video</button>
</div>
</div>
</footer>
<script src="sketch.js"></script>
</body>
</html>
CSS:
html,
body {
font-family: 'Nunito Sans', sans-serif;
color: #444444;
background-color:#e1f3f8;
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
/* align-items: stretch; */
height: 100%;
width: 100%;
}
/* Masthead */
#masthead {
text-align: center;
background-color:#e1f3f8;
}
/* Video */
#video {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
background-color: transparent;
}
/* Bottom toolbar */
#bottom-toolbar {
display: flex;
align-items: center;
height: 120px;
background-color:#e1f3f8;
padding: 10px;
}
#audio-controls {
flex: 1;
text-align: center;
}
#slider {
width: 100%; /* Full-width */
height: 25px; /* Specified height */
background: #d3d3d3; /* Grey background */
outline: none; /* Remove outline */
opacity: 0.7; /* Set transparency (for mouse-over effects on hover) */
position: fixed;
left: 0px;
top: 0px;
}
button {
background-color:white;
color: #444444;
padding: 8px;
border: 1px solid #444444;
border-radius: 8px;
position: relative;
left: 40px;
}
#play-button {
width: 150px;
}
span.redtext {
color: red;
}
span.bluetext {
color: blue;
}