如何让JavaScript发出哔声?

当用户超过我的<textarea>的最大字符数限制时,我希望我的网页发出嘟嘟声。

这是不可能的直接在JavaScript中。 您需要在HTML中embedded一个简短的WAV文件,然后通过代码播放。

一个例子:

 <script> function PlaySound(soundObj) { var sound = document.getElementById(soundObj); sound.Play(); } </script> <embed src="success.wav" autostart="false" width="0" height="0" id="sound1" enablejavascript="true"> 

然后你会从JavaScript代码中调用它:

 PlaySound("sound1"); 

这应该做你想要的 – 你只需要find/创build自己的嘟嘟声,这应该是微不足道的。

现在,您可以使用base64文件作为数据URI导入时发出声音。 该解决scheme与以前的解决scheme几乎相同,除了不需要导入外部audio文件。

 function beep() { var snd = new Audio("data:audio/wav;base64,//uQRAAAAWMSLwUIYAAsYkXgoQwAEaYLWfkWgAI0wWs/ItAAAGDgYtAgAyN+QWaAAihwMWm4G8QQRDiMcCBcH3Cc+CDv/7xA4Tvh9Rz/y8QADBwMWgQAZG/ILNAARQ4GLTcDeIIIhxGOBAuD7hOfBB3/94gcJ3w+o5/5eIAIAAAVwWgQAVQ2ORaIQwEMAJiDg95G4nQL7mQVWI6GwRcfsZAcsKkJvxgxEjzFUgfHoSQ9Qq7KNwqHwuB13MA4a1q/DmBrHgPcmjiGoh//EwC5nGPEmS4RcfkVKOhJf+WOgoxJclFz3kgn//dBA+ya1GhurNn8zb//9NNutNuhz31f////9vt///z+IdAEAAAK4LQIAKobHItEIYCGAExBwe8jcToF9zIKrEdDYIuP2MgOWFSE34wYiR5iqQPj0JIeoVdlG4VD4XA67mAcNa1fhzA1jwHuTRxDUQ//iYBczjHiTJcIuPyKlHQkv/LHQUYkuSi57yQT//uggfZNajQ3Vmz+Zt//+mm3Wm3Q576v////+32///5/EOgAAADVghQAAAAA//uQZAUAB1WI0PZugAAAAAoQwAAAEk3nRd2qAAAAACiDgAAAAAAABCqEEQRLCgwpBGMlJkIz8jKhGvj4k6jzRnqasNKIeoh5gI7BJaC1A1AoNBjJgbyApVS4IDlZgDU5WUAxEKDNmmALHzZp0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tfMH10000EEEEEECUBYln03TTTdNBDZopopYvrTTdNa325mImNg3TTPV9q3pmY0xoO6bv3r00y+IDGid/9aaaZTGMuj9mpu9Mpio1dXrr5HERTZSmqU36A3CumzN/9Robv/Xx4v9ijkSRSNLQhAWumap82WRSBUqXStV/YcS+XVLnSS+WLDroqArFkMEsAS+eWmrUzrO0oEmE40RlMZ5+ODIkAyKAGUwZ3mVKmcamcJnMW26MRPgUw6j+LkhyHGVGYjSUUKNpuJUQoOIAyDvEyG8S5yfK6dhZc0Tx1KI/gviKL6qvvFs1+bWtaz58uUNnryq6kt5RzOCkPWlVqVX2a/EEBUdU1KrXLf40GoiiFXK///qpoiDXrOgqDR38JB0bw7SoL+ZB9o1RCkQjQ2CBYZKd/+VJxZRRZlqSkKiws0WFxUyCwsKiMy7hUVFhIaCrNQsKkTIsLivwKKigsj8XYlwt/WKi2N4d//uQRCSAAjURNIHpMZBGYiaQPSYyAAABLAAAAAAAACWAAAAApUF/Mg+0aohSIRobBAsMlO//Kk4soosy1JSFRYWaLC4qZBYWFRGZdwqKiwkNBVmoWFSJkWFxX4FFRQWR+LsS4W/rFRb/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VEFHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAU291bmRib3kuZGUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjAwNGh0dHA6Ly93d3cuc291bmRib3kuZGUAAAAAAAAAACU="); snd.play(); } beep(); 

兼容性

现在几乎每个浏览器都支持数据URI。 有关http://caniuse.com/datauri的更多信息;

演示

http://jsfiddle.net/7EAgz/

我写了一个函数来用新的audioAPI发出嘟嘟声。

 var beep = (function () { var ctxClass = window.audioContext ||window.AudioContext || window.AudioContext || window.webkitAudioContext var ctx = new ctxClass(); return function (duration, type, finishedCallback) { duration = +duration; // Only 0-4 are valid types. type = (type % 5) || 0; if (typeof finishedCallback != "function") { finishedCallback = function () {}; } var osc = ctx.createOscillator(); osc.type = type; //osc.type = "sine"; osc.connect(ctx.destination); if (osc.noteOn) osc.noteOn(0); // old browsers if (osc.start) osc.start(); // new browsers setTimeout(function () { if (osc.noteOff) osc.noteOff(0); // old browsers if (osc.stop) osc.stop(); // new browsers finishedCallback(); }, duration); }; })(); 

jsFiddle 。

当时最重要的答案是正确的,但现在是错误的; 你可以在纯JavaScript中做到这一点。 但是,使用JavaScript的一个答案不再工作,其他答案是相当有限的或不使用纯JavaScript。

我自己的解决scheme运行良好,可以控制音量,频率和波形。

 //if you have another AudioContext class use that one, as some browsers have a limit var audioCtx = new (window.AudioContext || window.webkitAudioContext || window.audioContext); //All arguments are optional: //duration of the tone in milliseconds. Default is 500 //frequency of the tone in hertz. default is 440 //volume of the tone. Default is 1, off is 0. //type of tone. Possible values are sine, square, sawtooth, triangle, and custom. Default is sine. //callback to use on end of tone function beep(duration, frequency, volume, type, callback) { var oscillator = audioCtx.createOscillator(); var gainNode = audioCtx.createGain(); oscillator.connect(gainNode); gainNode.connect(audioCtx.destination); if (volume){gainNode.gain.value = volume;}; if (frequency){oscillator.frequency.value = frequency;} if (type){oscillator.type = type;} if (callback){oscillator.onended = callback;} oscillator.start(); setTimeout(function(){oscillator.stop()}, (duration ? duration : 500)); }; 

有人build议我编辑这个注意它只适用于某些浏览器。 然而,据我所知, Audiocontext似乎在所有现代浏览器上都得到支持 。 它在IE上不被支持,但是这已经被微软停止了。 如果您在特定的浏览器上有任何问题,请报告。

正如我们在这个答案中所看到的,如果你打开这个路线,HTML5将为你解决这个问题。 所有现代浏览器都支持HTML5audio。

这里是一个例子的副本:

 var snd = new Audio("file.wav"); // buffers automatically when created snd.play(); 

这将使您能够多次播放声音,与顶尖的答案相反:

 var playSound = (function beep() { var snd = new Audio("data:audio/wav;base64,//uQRAAAAWMSLwUIYAAsYkXg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d/+VJxZRRZlqSkKiws0WFxUyCwsKiMy7hUVFhIaCrNQsKkTIsLivwKKigsj8XYlwt/WKi2N4d//uQRCSAAjURNIHpMZBGYiaQPSYyAAABLAAAAAAAACWAAAAApUF/Mg+0aohSIRobBAsMlO//Kk4soosy1JSFRYWaLC4qZBYWFRGZdwqKiwkNBVmoWFSJkWFxX4FFRQWR+LsS4W/rFRb/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VEFHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAU291bmRib3kuZGUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjAwNGh0dHA6Ly93d3cuc291bmRib3kuZGUAAAAAAAAAACU="); return function() { snd.play(); } })(); playSound(); // Play first time playSound(); // Play second time 

使用Houshalter的build议 ,我做了这个简单的音色合成器演示。

截图

这是一个截图。 在这个答案中进一步尝试现场演示(单击运行代码片段 )。

现场演示的截图进一步下降在这个答案

演示代码

 audioCtx = new(window.AudioContext || window.webkitAudioContext)(); show(); function show() { frequency = document.getElementById("fIn").value; document.getElementById("fOut").innerHTML = frequency + ' Hz'; switch (document.getElementById("tIn").value * 1) { case 0: type = 'sine'; break; case 1: type = 'square'; break; case 2: type = 'sawtooth'; break; case 3: type = 'triangle'; break; } document.getElementById("tOut").innerHTML = type; volume = document.getElementById("vIn").value / 100; document.getElementById("vOut").innerHTML = volume; duration = document.getElementById("dIn").value; document.getElementById("dOut").innerHTML = duration + ' ms'; } function beep() { var oscillator = audioCtx.createOscillator(); var gainNode = audioCtx.createGain(); oscillator.connect(gainNode); gainNode.connect(audioCtx.destination); gainNode.gain.value = volume; oscillator.frequency.value = frequency; oscillator.type = type; oscillator.start(); setTimeout( function() { oscillator.stop(); }, duration ); }; 
 frequency <input type="range" id="fIn" min="40" max="6000" oninput="show()" /> <span id="fOut"></span><br> type <input type="range" id="tIn" min="0" max="3" oninput="show()" /> <span id="tOut"></span><br> volume <input type="range" id="vIn" min="0" max="100" oninput="show()" /> <span id="vOut"></span><br> duration <input type="range" id="dIn" min="1" max="5000" oninput="show()" /> <span id="dOut"></span> <br> <button onclick='beep();'>Play</button> 

使用CSS你可以做到这一点,如果你添加以下风格的标签,但你将需要一个wav文件:

 <style type="text/css"> .beep {cue: url("beep.wav") } </style> var body=document.getElementByTagName("body"); body.className=body.className + " " + "beep"; 

没有crossbrowser的方式来实现这个纯JavaScript。 相反,你可以使用一个小的.wav文件,使用embedded或对象标签播放。

以下是我如何使用HTML5发出嘟嘟声:首先我将Windows wav文件复制并转换为mp3,然后使用以下代码:

 var _beep = window.Audio("Content/Custom/Beep.mp3") function playBeep() { _beep.play()}; 

声明全球声音文件的速度更快,并根据需要进行引用。

 function Sound(url, vol, autoplay, loop) { var that = this; that.url = (url === undefined) ? "" : url; that.vol = (vol === undefined) ? 1.0 : vol; that.autoplay = (autoplay === undefined) ? true : autoplay; that.loop = (loop === undefined) ? false : loop; that.sample = null; if(that.url !== "") { that.sync = function(){ that.sample.volume = that.vol; that.sample.loop = that.loop; that.sample.autoplay = that.autoplay; setTimeout(function(){ that.sync(); }, 60); }; that.sample = document.createElement("audio"); that.sample.src = that.url; that.sync(); that.play = function(){ if(that.sample) { that.sample.play(); } }; that.pause = function(){ if(that.sample) { that.sample.pause(); } }; } } var test = new Sound("http://mad-hatter.fr/Assets/projects/FreedomWings/Assets/musiques/freedomwings.mp3"); test.play(); 

http://jsfiddle.net/sv9j638j/

注意:把这段代码放到你想要发生哔哔声的地方。 并记住指定保存嘟嘟声的目录或文件夹(来源)。

 <script> //Appending HTML5 Audio Tag in HTML Body $('<audio id="chatAudio"><source src="sound/notify.ogg" type="audio/ogg"><source src="sound/notify.mp3" type="audio/mpeg"><source src="sound/notify.wav" type="audio/wav"></audio>').appendTo('body'); $('#chatAudio')[0].play(); </script> 

参考: http : //www.9lessons.info/2013/04/play-notification-sound-using-jquery.html 。

我在开发的社交媒体上实现了这个function,并且在聊天的时候发现了一个类似Facebook的通知,通知你有一个新的聊天消息

您需要从某个地方提供声音文件。 以下是Scriptaculous的声音库的代码:

 //Default: <embed style="height:0" id="sound_#{track}_#{id}" src="#{url}" loop="false" autostart="true" hidden="true"/> //For Gecko: if(Prototype.Browser.Gecko && navigator.userAgent.indexOf("Win") > 0){ if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('QuickTime') != -1 })) Sound.template = new Template('<object id="sound_#{track}_#{id}" width="0" height="0" type="audio/mpeg" data="#{url}"/>'); else if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('Windows Media') != -1 })) Sound.template = new Template('<object id="sound_#{track}_#{id}" type="application/x-mplayer2" data="#{url}"></object>'); else if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('RealPlayer') != -1 })) Sound.template = new Template('<embed type="audio/x-pn-realaudio-plugin" style="height:0" id="sound_#{track}_#{id}" src="#{url}" loop="false" autostart="true" hidden="true"/>'); else Sound.play = function(){}; } 
 function beep(wavFile){ wavFile = wavFile || "beep.wav" if (navigator.appName == 'Microsoft Internet Explorer'){ var e = document.createElement('BGSOUND'); e.src = wavFile; e.loop =1; document.body.appendChild(e); document.body.removeChild(e); }else{ var e = document.createElement('AUDIO'); var src1 = document.createElement('SOURCE'); src1.type= 'audio/wav'; src1.src= wavFile; e.appendChild(src1); e.play(); } } 

适用于使用Win7操作系统的Chrome,IE,Mozilla。 需要服务器上的beep.wav文件。