Sound playback for a short beep effect


#1

Hello, Loving the React Studio software for my project.

My current new feature is that I am trying to playback a short beep sound effect in my web app when a new chat message appears. I imagine this might be difficult, since it might have to play audio across multiple devices and browsers. Would this even be possible?


#2

I actually got the start of something working for this based on some code I found online.

var audioCtx = new (window.AudioContext || window.webkitAudioContext || window.audioContext);


function beep(duration, frequency, volume) {
  
  if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
   //do nothing when mobile
  } else {
   
    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;}
  
  	oscillator.onended = function() { }
    oscillator.start(audioCtx.currentTime);
  	oscillator.stop(audioCtx.currentTime + ((duration || 250) / 1000));
    
  }
};

Then I call it with.

beep(250,1000,1);