Como posso fazer isso, para que sempre que um usuário clica em um link, reproduzamos um som? Usando javascript e jquery aqui.
.load(), .play()em momentos importantes, usando a API.
Como posso fazer isso, para que sempre que um usuário clica em um link, reproduzamos um som? Usando javascript e jquery aqui.
.load(), .play()em momentos importantes, usando a API.
Respostas:
Use este plugin: https://github.com/admsev/jquery-play-sound
$.playSound('http://example.org/sound.mp3');
Coloque um <audio>elemento em sua página.
Obtenha seu elemento de áudio e chame o play()método:
document.getElementById('yourAudioTag').play();
Confira este exemplo: http://www.storiesinflight.com/html5/audio.html
Este site revela alguns dos outros coisas legais que você pode fazer, como load(), pause()e algumas outras propriedades do elemento de áudio.
Quando exatamente você deseja reproduzir este elemento de áudio, depende de você. Leia o texto do botão e compare-o com "não", se desejar.
http://www.schillmania.com/projects/soundmanager2/
O SoundManager 2 fornece uma API fácil de usar que permite que o som seja reproduzido em qualquer navegador moderno, incluindo o IE 6+. Se o navegador não for compatível com HTML5, ele obtém ajuda do Flash. Se você quiser estritamente HTML5 e sem flash, há uma configuração para isso, prefere Flash = false
Suporta áudio 100% livre de Flash no iPad, iPhone (iOS4) e outros dispositivos + navegadores com HTML5
O uso é tão simples quanto:
<script src="soundmanager2.js"></script>
<script>
// where to find flash SWFs, if needed...
soundManager.url = '/path/to/swf-files/';
soundManager.onready(function() {
soundManager.createSound({
id: 'mySound',
url: '/path/to/an.mp3'
});
// ...and play it
soundManager.play('mySound');
});
Aqui está uma demonstração dele em ação: http://www.schillmania.com/projects/soundmanager2/demo/christmas-lights/
Encontrou algo assim:
//javascript:
function playSound( url ){
document.getElementById("sound").innerHTML="<embed src='"+url+"' hidden=true autostart=true loop=false>";
}
Usando a tag de áudio html5 e jquery:
// appending HTML5 Audio Tag in HTML Body
$('<audio id="chatAudio">
<source src="notify.ogg" type="audio/ogg">
<source src="notify.mp3" type="audio/mpeg">
</audio>').appendTo('body');
// play sound
$('#chatAudio')[0].play();
Codifique daqui .
Em minha implementação, adicionei o áudio embutido diretamente no HTML sem jquery append.
Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document firstVersion 70.0.3538.102 (Official Build) (64-bit)
Gerenciador de som JavaScript:
$('a').click(function(){
$('embed').remove();
$('body').append('<embed src="/path/to/your/sound.wav" autostart="true" hidden="true" loop="false">');
});
Eu escrevi uma pequena função que pode fazer isso, com a API de áudio da web ...
var beep = function(duration, type, finishedCallback) {
if (!(window.audioContext || window.webkitAudioContext)) {
throw Error("Your browser does not support Audio Context.");
}
duration = +duration;
// Only 0-4 are valid types.
type = (type % 5) || 0;
if (typeof finishedCallback != "function") {
finishedCallback = function() {};
}
var ctx = new (window.audioContext || window.webkitAudioContext);
var osc = ctx.createOscillator();
osc.type = type;
osc.connect(ctx.destination);
osc.noteOn(0);
setTimeout(function() {
osc.noteOff(0);
finishedCallback();
}, duration);
};
Novo emerger ... parece ser compatível com o IE, navegadores Gecko e iPhone até agora ...
O código a seguir pode ajudá-lo a reproduzir sons em uma página da web usando apenas javascript. Você pode ver mais detalhes em http://sourcecodemania.com/playing-sound-javascript-flash-player/
<script>
function getPlayer(pid) {
var obj = document.getElementById(pid);
if (obj.doPlay) return obj;
for(i=0; i<obj.childNodes.length; i++) {
var child = obj.childNodes[i];
if (child.tagName == "EMBED") return child;
}
}
function doPlay(fname) {
var player=getPlayer("audio1");
player.play(fname);
}
function doStop() {
var player=getPlayer("audio1");
player.doStop();
}
</script>
<form>
<input type="button" value="Play Sound" onClick="doPlay('texi.wav')">
<a href="#" onClick="doPlay('texi.wav')">[Play]</a>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
width="40"
height="40"
id="audio1"
align="middle">
<embed src="wavplayer.swf?h=20&w=20"
bgcolor="#ffffff"
width="40"
height="40"
allowScriptAccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer"
/>
</object>
<input type="button" value="Stop Sound" onClick="doStop()">
</form>
Em primeiro lugar, eu não gostaria disso como usuário.
A melhor maneira de fazer provavelmente é usar um pequeno miniaplicativo flash que reproduz o som de fundo.
Também respondido aqui: Forma multiplataforma e navegador cruzado de reproduzir som em Javascript?