Question

I'm revisiting an ID scanner station program we built ages ago and I have a request from users to make a system beep. We're considering moving the system to a web browser, but is it possible to invoke a speaker beep via javascript or something? It doesn't need to be cross-browser compatible, but it probably needs to work on Windows or Linux. The stations in question are not equipped with a soundcard or external speakers, hence the request for a PC speaker access.

I know someone's going to say it, so I'll address this up front: I don't care what you think about applications making noise, this isn't for you. Users request it, it makes sense, and the hardware scanner already makes noise anyways. Yes, we give visual feedback, with distinguishable text and color, but we find that people accept the existing beep as positive feedback and adding more audio context would help.

Was it helpful?

Solution

Using JavaScript, it's impossible - JavaScript has no access to the client computer except cookies and the new HTML5 local storage.

What you can do, however, is use a Java applet that will be controllable via JavaScript - hidden or not.

You can find an example here.

This requires Java runtime to be installed on the client computer.

OTHER TIPS

It's possible with JavaScript today.

Here's a quick & dirty function I wrote...

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

};

jsFiddle.

Try following way: It may easy for you....

function play_beep() {
  var snd = new Audio("http://www.externalharddrive.com/waves/computer/hello.wav");
  snd.play();
  return false;
}
<input type="submit" value="Play Beep" onclick="return play_beep();" />

I think your best bet would be a java applet doing the job...

This is not possible with native Javascript. You could possibly write an ActiveX control to do it, though.

Solution by Alex gave me Uncaught TypeError: osc.noteOn is not a function

This did it though:

Play = (function() {
  
  var ctx = new(AudioContext || webkitAudioContext);
  
  return function(duration, freq, finishedCallback) {
    duration = +duration;
    if (typeof finishedCallback != "function") {
      finishedCallback = function() {};
    }
    var osc = ctx.createOscillator();
    osc.type = 0;
    osc.connect(ctx.destination);
    osc.frequency.value = freq;
    
    if (osc.start) osc.start();
    else osc.noteOn(0);
    
    setTimeout(
      function() {
        if (osc.stop) osc.stop(0);
        else osc.noteOff(0);
        finishedCallback();
      }, duration
    );
  };
})();
Play(42, 666)

Took it from here

Hope this saves you some time

I honestly haven't tested this, but it would be worth a look,

Real Java's how to emit a beep but it does depend on you being able to ensure your client has an appropriate version of the JDK installed on every machine you are targeting.

Actually this is possible, probably due to new functions implemented in Java in the meantime. Here is a short example:

var context = new AudioContext();
var o = context.createOscillator();
o.type = "sine";
o.connect(context.destination);
o.start();
setTimeout(function(){ 
        o.stop();
}, 100);

To get more, please visit this site https://marcgg.com/blog/2016/11/01/javascript-audio/ where I found the solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top