I am attempting to load various sounds (.mp3 audio) on separate audio players that are displayed on a single HTML page. The number of players displayed on the screen is determined by the length of the array. In this specific example, I have 3 elements in the array, resulting in 3 audio players playing different sounds.
I have successfully positioned 3 players vertically on the page, each corresponding to their respective texts (Animal sounds). However, I am encountering the following issues:
Each player plays the same sound instead of incrementing and playing each sound for its designated player. I attempted to use a "for loop" to iterate through the array, but I could not find a way to make it remember the previous audio, causing it to always play the last sound for all players. In this case, only the sound "Cat" is played.
Even when clicking on an empty area between the first and second player where there is no audio player, the sound still plays, and I am unsure as to why...
Any assistance would be greatly appreciated!!!
// Utilize JQuery to select the audio ID and play the audio
jQuery(document).ready(function() {
var speakWord = document.getElementsByClassName('speakout');
var nowPlaying = speakWord[0];
nowPlaying.load();
$("#divAudio").on("click", function() {
nowPlaying.play();
});
});
var textBox = document.getElementById('inBox'); // Responsible for displaying words
var player = document.getElementById('myPlayer');// Playing the sound (<audio>)
var outArr = ['Dog', 'Cow','Cat'];
var pathArr = ['http://cd.textfiles.com/mmplus/MEDIA/WAV/EFFECTS/BARK.WAV', 'http://www.internetstart.se/download/ljud/moo.wav', 'http://area512.htmlplanet.com/wavs/blackcat.wav'];
var audioLogo = document.getElementById('divAudio');// Resposible for the appearance of the audio player (LOGO)
var img_audio = document.createElement("IMG");// Properties of the IMAGE
br = document.createElement("br"); // Creating a break in the document
var new_audio = document.createElement("audio");
var points = 50;
if(outArr.constructor === Array) {
//audioLogo.style.display = "initial";
for(i=0; i < outArr.length; ++i) {
// Regarding the TEXT elements:
textBox.innerHTML += outArr[i]+'<br>';
textBox.style.fontSize = "30px";
textBox.style.position = "absolute";
textBox.style.top = String(points)+'pt';
textBox.style.marginRight= "5pt";
textBox.style.lineHeight = "71pt";
textBox.style.right = '5pt';
}
}
else {
textBox.innerHTML += outArr;
textBox.style.fontSize = "30px";
textBox.style.position = "absolute";
textBox.style.top = "76pt";
}
var points = 70;
for(i=0; i < pathArr.length; ++i) {
var new_audio = document.createElement("audio");
var multiAud = audioLogo.querySelectorAll('.multiple_audio');
imgArr = Array(pathArr.length).fill('http://www.coli.uni-saarland.de/~andreeva/powin/images/sound.png');
if(i<1){
img_audio.style.height = "9%"; // The size of the audio logo
img_audio.style.width = "9%";
img_audio.setAttribute("src", imgArr[i]); // Creating an attribute (image) to be added to doc
audioLogo.style.position = "absolute";
audioLogo.style.top = '58pt';
audioLogo.style.lineHeight = "73pt";
player.src = pathArr[i];
audioLogo.appendChild(img_audio);
audioLogo.innerHTML += '<br>';
}
else {
var audio = new Audio(pathArr[i]);
audio.className = 'multiple_audio';
img_audio.style.height = "9%";
img_audio.style.width = "9%";
img_audio.setAttribute("src", imgArr[i]); // Creating an attribute (image) to be added to doc
audioLogo.style.position = "absolute";
audioLogo.style.top = '58pt';
audioLogo.style.lineHeight = "73pt";
audioLogo.appendChild(img_audio);
player.src = pathArr[i];
audioLogo.innerHTML += '<br>';
// player.parentNode.insertBefore(audio , player.nextNode);
}
}
#layout {
position: relative;
overflow: auto;
left: 225px;
width: 800px;
height: 370px;
background-color: white;
padding-left: 1px;
max-width:100%;
}
#myText {
pointer-events: none;
width: 800px;
height: 370px;
resize: none;
font-size: 45px;
font-family: Arial, Helvetica, Verdana;
background: url(https://qph.is.quoracdn.net/main-qimg-47327da6ae3e0b3727a9b9a7ea7f1adb?convert_to_webp=true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script type="text/javascript" src="jquery-1.11.3.js"></script>
</head>
<body>
<br>
<div id="layout" readonly="readonly">
<div contenteditable maxlength="200" readonly="readonly" id="myText" dir="auto" name="outtest" class = "fetchBox"></div>
<div id="inBox" class="fetch" dir="auto"></div>
<audio class="speakout" id="myPlayer">
Your Browser does not support the HTML audio Tag!
</audio>
<div class="play" id="divAudio"><img id="play_image"> </div>
</div>
</body>
</html>
P.S
The audio tags should not display the standard play/pause controls by default. I customized my own player logo; however, for this example, I used a logo from an absolute URL.
I attempted to implement suggestions from another post (multiple and dynamic audio players applied for a dictionary in HTML/JavaScript) but encountered challenges. I tried utilizing
var audio = new Audio(); and
player.parentNode.insertBefore(audio , player.nextNode);
to address problem 1. It is unclear whether the issue stems from not using the standard player?