Simple oscillator but no sound with web audio API on iOS

Here is a super simple example that I am trying to run on an iphone in chrome. Other examples of web audio APIs, such as this http://alxgbsn.co.uk/wavepad/ , work, but not mine :(

<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script> var audioContext, osc audioContext = new (window.AudioContext || window.webkitAudioContext); osc = audioContext.createOscillator() osc.connect(audioContext.destination) if (osc.noteOn) osc.start = osc.noteOn osc.start(0) osc.frequency.value = 440 </script> </body> </html> 

Any idea what's wrong?

EDIT

To summarize the answer:

  • start sound in response to user interaction
  • check if the mute switch is off.
+8
javascript web-audio
source share
1 answer

iOS allows you to play sound using the Web Audio API as a result of user action.

Try putting this code inside some event handler.

 var elem = document.getElementById('play'), audioContext = new (window.AudioContext || window.webkitAudioContext), osc = audioContext.createOscillator(); osc.connect(audioContext.destination); if (osc.noteOn) osc.start = osc.noteOn osc.frequency.value = 440; play.addEventListener('click', function() { osc.start(0); }, false); 
+10
source share

All Articles