How to make this code can be used more than six times

Asked

Viewed 40 times

-5

<!DOCTYPE HTML>
<head>
 <Title>Api de som</title>
</head>
<body>
<button onclick="play()">play</button>

<Script>
let context = new AudioContext();
oscillator = context.createOscillator();
oscillator.type="sine";
oscillator.connect(context.destination);
oscillator.frequency.value=2000;

function play(){
oscillator.start(0);
oscillator.stop(0.5);
}
</Script>
</body>
</html>

1 answer

2

Reading the error log is being reported:

"message": "Uncaught Invalidstateerror: Failed to execute 'start' on 'Audioscheduledsourcenode': cannot call start more than Once."

In other words, there was a failure to execute the method OscillatorNode.start() because the method cannot be called more than once per oscillator.

Then migrate to the body of the function play() the methods of creating and configuring the oscillator.

function play() {
  let context = new AudioContext();
  oscillator = context.createOscillator();

  oscillator.type = "sine";
  oscillator.connect(context.destination);
  oscillator.frequency.value = 2000;

  oscillator.start(0);
  oscillator.stop(0.5);
}
<!DOCTYPE HTML>

<head>
  <Title>Api de som</title>
</head>

<body>
  <button onclick="play()">play</button>
</body>

</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.