Microsoft Edge browser embed audio shows buzz icon - javascript

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");

Related

How to play sound in javascript?

I create websocket server in python to handle notification event. Now, i can receive notification, the problem is i can't play sound because new autoplay policy changed, if i play sound using javascript it give me domexception. Any suggestion please ?
As i know, playing sound is simple in html-javascript. like this example: https://stackoverflow.com/a/18628124/7514010
but it depend to your browsers and how you load and play, so issues is:
Some of browsers wait till user click something, then let you play it (Find a way for it)
In some case browsers never let you play till the address use SSL (means the HTTPS behind your url)
The loading be late so the playing be late / or even not start.
So i usually do this:
HTML
<audio id="notifysound" src="notify.mp3" autobuffer preload="auto" style="visibility:hidden;width:0px;height:0px;z-index:-1;"></audio>
JAVASCRIPT (Generally)
var theSound = document.getElementById("notifysound");
theSound.play();
And the most safe if i want sure it be played when i notify is :
JAVASCRIPT (In your case)
function notifyme(theTitle,theBody) {
theTitle=theTitle || 'Title';
theBody=theBody || "Hi. \nIt is notification!";
var theSound = document.getElementById("notifysound");
if ("Notification" in window && Notification) {
if (window.Notification.permission !== "granted") {
window.Notification.requestPermission().then((result) => {
if (result != 'denied') {
return notifyme(theTitle,theBody);
} else {
theSound.play();
}
});
} else {
theSound.play();
try {
var notification = new Notification(theTitle, {
icon: 'icon.png',
body: theBody
});
notification.onclick = function () {
window.focus();
};
}
catch(err) {
return;
}
}
} else {
theSound.play();
}
}
(and just hope it be played. because even possible to volume or some customization make it failed.)
to bypass new autoplay policy :
create a button that can play the sound, hide it and trigger the sound with :
var event = new Event('click');
playBtn.dispatchEvent(event);
EDIT
assuming you have :
let audioData = 'data:audio/wav;base64,..ᴅᴀᴛᴀ...'; // or the src path
you can use this function to trigger whenever you want without appending or create element to the DOM:
function playSound() {
let audioEl = document.createElement('audio');
audioEl.src = audioData;
let audioBtn = document.createElement('button');
audioBtn.addEventListener('click', () => audioEl.play(), false);
let event = new Event('click');
audioBtn.dispatchEvent(event);
}
usage :
just playSound()
EDIT 2
I re test my code and it does'nt work hum ... weird

safari ios audio- refusing to play with error

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).

webrtc remote video on firefox not work

iam use webrtc for made an video call when i made an call between chrome and chrome work without problem
but when i made call with firefox ... firefox share video to chrome but video from chrome not shown in firefox i think there are problem in attach mediastream for firefox because
the video element src read this
<video id="remoteVideo" autoplay="" src="[object MediaStream]"></video>
and in console of firefox its show this
404 failed to load http://localhost/videocall/[object%20MediaStream].
and this is my code for attach stream to video element
pc.onaddstream = function(event) {
var ts = document.getElementById("remoteVideo");
attachMediaStreamx(ts, event.stream);
};
attachMediaStreamx = function(element, stream) {
if (typeof element.srcObject !== 'undefined') {
element.srcObject = stream;
} else if (typeof element.mozSrcObject !== 'undefined') {
element.mozSrcObject = stream;
} else if (typeof element.src !== 'undefined') {
element.src = URL.createObjectURL(stream);
} else {
console.log('Error attaching stream to element.');
}
};
Just try
pc.onaddstream = function(event) {
remoteVideo.srcObject = event.stream;
};
You should not have src="[object MediaStream]" in your HTML code. Just remove that bit. It is probably interfering with the srcObject.

play background music on page load on Safari

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.

JS: Audio Method not implemented in IE 10 of server 2008 R2

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.

Categories