I have tried to add audio to a webpage of mine which is supposed to run automatically once the page is loaded, but it doesn't work.
<!DOCTYPE html>
<html>
<head><title>Feedback</title>
<style>
body{
font-family: Calibri, 'Gill Sans', 'Gill Sans MT', 'Trebuchet MS', sans-serif;
align-items: center;
justify-content: center;
background-image: url(back.png);
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
#input{
padding: 2px;
font-style: Calibri;
border: 2px solid black;
border-radius: 2px;
transition: 0.3s ease;
}
#input:focus{
color: white;
background: rgba(0,0,50.2, 0.5);
}
.form{
margin: auto;
align-items: center;
justify-content: center;
max-width: max-content;
padding: 2% 4%;
background-color: white;
border-radius: 30px;
box-shadow: -15px -15px 15px rgba(225, 225, 225, 0.2),
15px 15px 15px rgba(0,0,0,0.1),
-10px -50px 50px rgba(0, 0, 0, 0.2),
50px 50px 50px rgba(0,0,0,0.1);
margin-top:4%;
animation: top 1s;
}
input[type=Submit], input[type=Reset]{
padding: 9px 12px;
color: white;
background-color: navy;
border: none;
border-radius: 5px;
cursor: pointer;
transition: 0.3s ease;
font-family: comic sans MS;
font-weight: bold;
}
input[type=Submit]:hover, input[type=Reset]:hover{
background-color: #047acf;
}
@keyframes top{
from{
opacity: 0;
margin-top: -10%;
}
to{
opacity: 1;
margin-top: 4%;
}
}
</style></head>
<body>
<div class="form">
<h2>Please provide the following information</h2>
<form action="mailto: myid@gmail.com" method="post">
<p>Enter your name:* <input type="text" name="urname" size="30" maxlength="20" id="input"required><br><br>
Address:* <input type="text" name="address" maxlength="50" id="input"required><br><br>
City:* <input type="text" name="city" size="20" maxlength="20" id="input"required><br><br>
Mobile:* <input type="number" name="mobile" size="10" maxlength="10" id="input"required><br><br>
<br>
<br>
How much would you rate this website?*
<br>
<input type="radio" name="rate" id="radio1" required>
<label for="radio1">Excellent</label>
<input type="radio" name="rate" id="radio2"required>
<label for="radio2">Good</label>
<input type="radio" name="rate" id="radio3"required>
<label for="radio3">Average</label>
<input type="radio" name="rate" id="radio4"required>
<label for="radio4">Bad</label>
<input type="radio" name="rate" id="radio5"required>
<label for="radio">Very bad</label>
<br>
<br>
Comments:*<br> <textarea name="comments" id="input" cols="50" rows="4" wraprequired></textarea><br><br>
<input type='Submit' value ="SUBMIT">
<input type='Reset' value ="RESET" ></p>
</form>
</div>
<audio autoplay id="aud">
<source src="videoplayback.mp3" type="audio/mpeg">
</audio>
</body>
</html>
What I have tried:
I have tried starting the audio via javascript, but I get the error:
Quote:
Uncaught TypeError: Cannot read property 'play' of null
The script:
<!DOCTYPE html>
<html>
<head><title>Feedback</title>
<style>
body{
font-family: Calibri, 'Gill Sans', 'Gill Sans MT', 'Trebuchet MS', sans-serif;
align-items: center;
justify-content: center;
background-image: url(back.png);
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
#input{
padding: 2px;
font-style: Calibri;
border: 2px solid black;
border-radius: 2px;
transition: 0.3s ease;
}
#input:focus{
color: white;
background: rgba(0,0,50.2, 0.5);
}
.form{
margin: auto;
align-items: center;
justify-content: center;
max-width: max-content;
padding: 2% 4%;
background-color: white;
border-radius: 30px;
box-shadow: -15px -15px 15px rgba(225, 225, 225, 0.2),
15px 15px 15px rgba(0,0,0,0.1),
-10px -50px 50px rgba(0, 0, 0, 0.2),
50px 50px 50px rgba(0,0,0,0.1);
margin-top:4%;
animation: top 1s;
}
input[type=Submit], input[type=Reset]{
padding: 9px 12px;
color: white;
background-color: navy;
border: none;
border-radius: 5px;
cursor: pointer;
transition: 0.3s ease;
font-family: comic sans MS;
font-weight: bold;
}
input[type=Submit]:hover, input[type=Reset]:hover{
background-color: #047acf;
}
@keyframes top{
from{
opacity: 0;
margin-top: -10%;
}
to{
opacity: 1;
margin-top: 4%;
}
}
</style></head>
<body>
<div class="form">
<h2>Please provide the following information</h2>
<form action="mailto: myid@gmail.com" method="post">
<p>Enter your name:* <input type="text" name="urname" size="30" maxlength="20" id="input"required><br><br>
Address:* <input type="text" name="address" maxlength="50" id="input"required><br><br>
City:* <input type="text" name="city" size="20" maxlength="20" id="input"required><br><br>
Mobile:* <input type="number" name="mobile" size="10" maxlength="10" id="input"required><br><br>
<br>
<br>
How much would you rate this website?*
<br>
<input type="radio" name="rate" id="radio1" required>
<label for="radio1">Excellent</label>
<input type="radio" name="rate" id="radio2"required>
<label for="radio2">Good</label>
<input type="radio" name="rate" id="radio3"required>
<label for="radio3">Average</label>
<input type="radio" name="rate" id="radio4"required>
<label for="radio4">Bad</label>
<input type="radio" name="rate" id="radio5"required>
<label for="radio">Very bad</label>
<br>
<br>
Comments:*<br> <textarea name="comments" id="input" cols="50" rows="4" wraprequired></textarea><br><br>
<input type='Submit' value ="SUBMIT">
<input type='Reset' value ="RESET" ></p>
</form>
</div>
<audio autoplay id="aud">
<source src="videoplayback.mp3" type="audio/mpeg">
</audio>
<script>
window.onload = ()=>{document.querySelector('#aud').play();}
</script>
</body>
</html>