I want to play background music on page load.
My code runs fine on all browsers except Safari.
Safari shows.
Undefined is not constructor(evaluating new audio())
How can I fix this error on Safari?
var audio, playbtn, mutebtn, seek_bar;
function initAudioPlayer(){
audio = new Audio();
audio.src = "html/audio/di-evantile_behind-your-dream.mp3";
audio.loop = true;
audio.play();
// Set object references
playbtn = document.getElementById("playpausebtn");
mutebtn = document.getElementById("mutebtn");
// Add Event Handling
playbtn.addEventListener("click",playPause);
mutebtn.addEventListener("click", mute);
// Functions
function playPause(){
if(audio.paused){
audio.play();
playbtn.style.background = "url(html/images/pause.png) no-repeat";
} else {
audio.pause();
playbtn.style.background = "url(html/images/play.png) no-repeat";
}
}
function mute(){
if(audio.muted){
audio.muted = false;
mutebtn.style.background = "url(html/images/speaker.png) no-repeat";
} else {
audio.muted = true;
mutebtn.style.background = "url(html/images/speaker_muted.png) no-repeat";
}
}
}
//play music on page load
window.addEventListener("load", initAudioPlayer);
You seem to have a strange comment in your JavaScript. I had to remove "## Heading ##" in order for your script to run on my machine.
Regarding Safari:
Safari for Windows was discontinued back in 2012. I wouldn't be surprised if all sorts of HTML5 goodies didn't work in the Windows version.
It's working for me on a Mac OSX system with Safari 9.1.1 (11601.6.17).
I also tried using Safari for Windows as you described, and I can confirm that it doesn't work because the Audio object is not supported.
Related
I tried MediaSource API but it's not working correctly due to the latest browser does not support some of the functions from MediaSource API for example webkitMediaSourceURL.
https://wwwhtml5rockscom.readthedocs.io/en/latest/content/tutorials/streaming/multimedia/en/
I'm looking for open source one javascript.
I tried another example but There is a problem with seamless playback, a gap between two chunks is noticeable.
var player = document.querySelector('video');
getBlob('webm-chunk1')
.then(function (chunk1) {
var url = URL.createObjectURL(chunk1);
player.src = url;
player.play();
return getBlob('webm-chunk2');
})
.then(function (chunk2) {
var url = URL.createObjectURL(chunk2);
player.addEventListener('ended', handler, false);
function handler() {
player.removeEventListener('ended', handler);
player.src = url;
player.load();
player.play();
}
});
I can not play audio on autoplay in the new version chrome br!
Help me guys
const myAudio = new Audio('transient.mp3');
myAudio.loop = true;
myAudio.addEventListener('ended', function () {
this.currentTime = 0;
this.play();
}, false);
myAudio.play();
Because the user will need to have interacted with the domain before music can play. See https://developers.google.com/web/updates/2017/09/autoplay-policy-changes
The javascript error is: Unhandled Promise Rejection: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.
My setup works across other browsers, desktop and mobile.
The way it works is:
have a flag first_audio_played = false;
add a touch event listener that plays some audio, and sets first_audio_played = true; (then removes the touch listener)
all subsequent audio checks if(first_audio_played) some_other_audio.play();
this way, only the first audio played requires direct user input. after that, all audio is free to be triggered by in-game events, timing, etc...
this appears to be the "rule" for audio across most browsers. is the iOS "rule" that every audio needs to be triggered by user input? or is there some other step I'm missing?
For my javascript game, sounds stopped working on iOS recently. They all have readyState=4, but only the sound I played on tap works, the others won't play. Maybe you could play all the sounds on the first tap. But the solution I found that works for now for me is to load all the sounds from ajax arraybuffers and use decodeAudioData(). Then once you've played 1 sound from user tap (on not the body), they all play whenever.
Here is my working code where the second way of doing it is on bottom. When I tap to play sound2, sound1 starts working also.
<html>
<body>
<div id=all style='font-size:160%;background:#DDD' onclick="log('clicked');playSound(myAudio)">
Sound1 should be playing every couple seconds.
<br />Tap here to play sound1.
</div>
<div id=debug style='font-size:120%;' onclick="playSound(myAudio2)">
Tap here to play the sound2.
</div>
<script>
var url = "http://curtastic.com/drum.wav"
var url2 = "http://curtastic.com/gold.wav"
var myAudio, myAudio2
if(0)
{
var playSound = function(sound)
{
log("playSound() readyState="+sound.readyState)
log("gold readyState="+myAudio2.readyState)
sound.play()
}
var loadSound = function(url, callback)
{
var audio = new Audio(url)
audio.addEventListener('canplaythrough', function()
{
log('canplaythrough');
if(callback)
callback()
}, false)
audio.load()
if(audio.readyState > 3)
{
log('audio.readyState > 3');
if(callback)
callback()
}
return audio
}
myAudio = loadSound(url, startInterval)
myAudio2 = loadSound(url2)
}
else
{
var playSound = function(sound)
{
log("playSound()")
var source = audioContext.createBufferSource()
if(source)
{
source.buffer = sound
if(!source.start)
source.start = source.noteOn
if(source.start)
{
var gain = audioContext.createGain()
source.connect(gain)
gain.connect(audioContext.destination)
source.start()
}
}
}
var loadSound = function(url, callback)
{
log("start loading sound "+url)
var ajax = new XMLHttpRequest()
ajax.open("GET", url, true)
ajax.responseType = "arraybuffer"
ajax.onload = function()
{
audioContext.decodeAudioData(
ajax.response,
function(buffer)
{
log("loaded sound "+url)
log(buffer)
callback(buffer)
},
function(error)
{
log(error)
}
)
}
ajax.send()
}
var AudioContext = window.AudioContext || window.webkitAudioContext
var audioContext = new AudioContext()
loadSound(url, function(r) {myAudio = r; startInterval()})
loadSound(url2, function(r) {myAudio2 = r})
}
function startInterval()
{
log("startInterval()")
setInterval(function()
{
playSound(myAudio)
}, 2000)
}
function log(m)
{
console.log(m)
debug.innerHTML += m+"<br />"
}
</script>
</body>
</html>
You can use either [WKWebViewConfiguration setMediaTypesRequiringUserActionForPlayback:WKAudiovisualMediaTypeNone] or [UIWebView setMediaPlaybackRequiresUserAction:NO] depending on your WebView class (or Swift equivalent).
I use this function for play sound
var soundObject = null;
function PlaySound() {
if (soundObject != null) {
document.body.removeChild(soundObject);
soundObject.removed = true;
soundObject = null;
}
soundObject = document.createElement("embed");
//soundObject.setAttribute("src", "../Sounds/beep-01a1.wav");
soundObject.setAttribute("src", "../Sounds/Small Blink.mp3");
soundObject.setAttribute("hidden", true);
soundObject.setAttribute("autostart", true);
document.body.appendChild(soundObject);
// alert('hii sound');
}
it works fine in all browser instead of Microsoft Edge browser it shows buzzer icon in browser
Any help is appreciable, Thanks.
The Edge browser seemingly ignores the "hidden" attribute. Try this:
soundObject.setAttribute("style", "display:none");
Here is my script:
<script>
var bgMusic = new Audio("../song.mp3");
var bgMusic2 = new Audio("");
</script>
IE10 of server 2008 R2 throw this error: Not implemented.
I tried in IE10 of normal desktop like window 8 and no this error.
Please help, Thanks!!
If sound support has been disabled (the default in windows server), then IE throws an error when trying to access the html5 Audio object.
You could use Try Catch block to test for audio support like this:
//test file type support:
var snd = null;
var mp3support = false;
var oggsupport = false;
try {
snd = new Audio();
if (snd.canPlayType('audio/ogg')) {
//play your ogg file
oggsupport = true;
}
else if (snd.canPlayType('audio/mp3')) {
//play your mp3 file
mp3support = true;
}
} catch(e) {
return;
}
I have tried this and it works great.