I have been trying to get an oscillator to play in a mobile browser on IOS (won't work in Chrome or Safari) and I am struggling. From the research I have done I have found that you must create the oscillator (and maybe even the context) on a touch event. What I have working on desktop is an oscillator that connects to a gain node and plays a sound on a mouseenter event on a span element. Then on a mouseout event it disconnects from the gain node so that on the next mouseenter event it will connect again thus being able to create a new sound every time a character is hovered on.
$(".hover-letters span").on("touchend mouseenter", function(){
audioVariables($(this));
});
$(".hover-letters span").on("touchstart mouseout", function(){
oscillator.disconnect(gainNode);
});
function audioVariables(element){
resizeWindowWidth = parseInt($(window).width());
frequency = mouseX/(resizeWindowWidth/600);
type = element.parent().data("type");
sound();
}
var firstLetterInteraction = true;
function sound() {
if (firstLetterInteraction === true){
audioCtx = new(window.AudioContext || window.webkitAudioContext)();
oscillator = audioCtx.createOscillator();
gainNode = audioCtx.createGain();
oscillator.start();
}
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
oscillator.frequency.value = frequency;
oscillator.type = type;
firstLetterInteraction = false;
};
For some reason the sound just won't play on IOS and is not showing any errors. I'm beginning to wonder if this is possible as even examples such as CaptureWiz example here:
How do I make Javascript beep?
and the example given on the Web Audio API site: http://webaudioapi.com/samples/oscillator/ do not work for me on mobile. Anybody have any ideas?