I am a student and am a beginner of Web APIs. I wrote this code for a practice of generating simple tones using JavaScript:
<html>
<body>
Frequency = <input type="number" id="freq"/><br>
Waveform = <input type="text" id="wf"/><br>
<button onclick="start()">Play</button>
<button onclick="stop()">Stop</button>
</body>
<script>
const context=new AudioContext();
const oscillator=context.createOscillator();
oscillator.type='sine';
oscillator.frequency.value=400;
const gain=context.createGain();
oscillator.connect(gain);
gain.connect(context.destination);
function start()
{
oscillator.start();
}
function stop()
{
oscillator.stop();
}
</script>
</html>
The problem:
There are two buttons, Play and Stop. When I click on the Play button the tone is playing. Then I am clicking on the Stop button to stop the tone. Then, again when I am clicking on the Play button, no sound is heard!
What have I done wrong?