Guys ok so I’ve created a sample website with 3 examples. All of them with audio urls extracted from a simple database.
Example 1 - Using an html component as a button, it plays the audios in a loop (the next audio is played everytime the button is clicked).
Example 2 - Using an html component as a button, it plays an audio everytime the button is clicked.
Example 3 - Using an html component + a wix button, it plays an audio everytime the wix button is clicked.
Here’s the sample website: https://matheusmvl.wixsite.com/audioexample
See the codes with comments bellow. Hope it helps!
Html component #html1
<!DOCTYPE html>
<html>
<head>
<style>
.button {
background-color: dodgerblue;
border: none;
color: white;
padding: 15px 35px;
text-align: center;
font-size: 16px;
cursor: pointer;
border-radius: 8px;
outline: none;
}
.button:hover {
background-color: #2B689C;
}
</style>
</head>
<!– When the html component finishes loading, it executes this function that sends a message to the page –>
<body onload="htmlIsReady()">
<script type="text/javascript">
//Array that will hold the audio objects
var playList = [];
//Counter to know which audio index to play
var k = 0;
//Array that will receive the audio urls from the page
var audioLinks = [];
//Upon receving a message from the page...
window.onmessage = (event) => {
if (event.data) {
//Pass the audio urls array to our new array
audioLinks = event.data;
//Reset the counter
k = 0;
//Create the new audio objects using the audio urls received
for(var i = 0; i < audioLinks.length; i++)
{
playList[i] = new Audio();
playList[i].src = audioLinks[i];
}
}
};
//Function that sends a message to the page
function htmlIsReady() {
window.parent.postMessage("html_is_ready", "*");
}
//Function that controls the loop and also plays the audio
function playNext() {
if(k == playList.length)
k = 0;
playList[k].play();
k++;
}
</script>
<!– Button that executes the function that plays the audio everytime it's clicked –>
<button class="button" onclick="playNext()">Play Next (html1)</button>
</body>
</html>
Html component #html2
<!DOCTYPE html>
<html>
<head>
<style>
.button {
background-color: dodgerblue;
border: none;
color: white;
padding: 15px 25px;
text-align: center;
font-size: 16px;
cursor: pointer;
border-radius: 8px;
outline: none;
}
.button:hover {
background-color: #2B689C;
}
</style>
</head>
<!– When the html component finishes loading, it executes this function that sends a message to the page –>
<body onload="htmlIsReady()">
<script type="text/javascript">
//Variable that will be the audio object
var audio;
//Variable that will receive the audio url
var audioLink;
//Upon receing a message from the page...
window.onmessage = (event) => {
if (event.data) {
//Passes the audio url to the new variable
audioLink = event.data;
//Created a new audio object with the received audio url
audio = new Audio(audioLink);
}
};
//Function that sends a message to the page
function htmlIsReady() {
window.parent.postMessage("html_is_ready", "*");
}
//Funcion that plays the audio
function playAudio() {
audio.play();
}
</script>
<!– Button that executes the function that plays the audio everytime it's clicked –>
<button class="button" onclick="playAudio()">Play (html2)</button>
</body>
</html>
Html component #html3
<!DOCTYPE html>
<html>
<body style="background-color:dodgerblue; color:white;font-size: 17px">
<p style="text-align:center;">HTML COMPONENT</p>
<p style="text-align:center;">INTERACT WITH IT BY CLICKING ANYWHERE INSIDE IT OR CHROME WON'T ALLOW THE AUDIO TO PLAY</p>
<p style="text-align:center;">(try clicking the button before clicking here)</p>
<script type="text/javascript">
//Upon receiving a message...
window.onmessage = (event) => {
if (event.data) {
//Creates a new variable to hold the received audio url
var audioLink = event.data;
//Creates a new audio object using the audio url and plays it
var audio = new Audio(audioLink);
audio.play();
}
};
</script>
</body>
</html>
Page code
$w.onReady(function () {
$w("#audioDataBase").onReady( () => {
//Get the total number of items in the database
const numberOfAudios = $w("#audioDataBase").getTotalCount();
//Get all the database items
$w("#audioDataBase").getItems(0, numberOfAudios)
.then( (result) => {
let items = result.items;
//Retrieve all the audio urls from the items array and puts them into a new array
var audioURLs = items.map(item => item["audioLink"]);
//Upon receiving the message from the html component "html1" telling it's loaded, the page sends it the audio urls array and
//shows the html component, which in this case is a button.
$w("#html1").onMessage( (event, $w) => {
$w('#html1').postMessage(audioURLs);
$w('#html1').show();
} );
//Upon receiving the message from the html component "html2" telling it's loaded, the page sends it the audio url of the first
//item of the array and then shows the html component, which in this case is also a button.
$w("#html2").onMessage( (event, $w) => {
$w('#html2').postMessage(items[0].audioLink);
$w('#html2').show();
} );
//Upon clicking the button1, it sends the html component "html3" a message with the audio url of the first item of the array.
//In this case the html component is not a button, but simple text.
$w("#button1").onClick( (event, $w) => {
$w('#html3').postMessage(items[0].audioLink);
} );
} )
.catch( (err) => {
let errMsg = err.message;
let errCode = err.code;
} );
} );
});