简体   繁体   English

Web 音频 API 振荡器 onstarted 事件

[英]Web audio API oscillator onstarted event

The Web Audio API oscillator allows a script to be alerted when the oscillator stops with onended . Web Audio API振荡器允许在振荡器以onended停止时向脚本发出警报。

How can the script be alerted when it starts?脚本启动时如何发出警报?

const ac = new AudioContext()

const osc = ac.createOscillator()
osc.type = 'sawtooth'
osc.connect(ac.destination)

osc.onstarted = () => console.log('ended!') // This doesn't work :'(
osc.onended = () => console.log('ended!') // :)

osc.start(5)
osc.stop(7)

The ended event is the only event that an OscillatorNode emits. ended事件是OscillatorNode发出的唯一事件。 I guess this is because this event is inherited from the AudioScheduledSourceNode interface.我猜这是因为这个事件是继承自AudioScheduledSourceNode接口的。

Another node which inherits that interface is the AudioBufferSourceNode .继承该接口的另一个节点是AudioBufferSourceNode And for an AudioBufferSourceNode it's often not so easy to tell when it actually ends which is why I think the event exists.对于AudioBufferSourceNode来说,通常很难判断它何时真正结束,这就是我认为该事件存在的原因。 This could also be the reason why there is no started event.这也可能是没有started事件的原因。

Anyway you can re-build that functionality by using another AudioScheduledSourceNode .无论如何,您可以使用另一个AudioScheduledSourceNode重新构建该功能。 I would recommend to use a ConstantSourceNode since it is the one which is the most lightweight.我建议使用ConstantSourceNode ,因为它是最轻量级的。

const constantSourceNode = new ConstantSourceNode(ac);

constantSourceNode.onended = () => {
    console.log('osc started');
};
constantSourceNode.start();
constantSourceNode.stop(5);

Please note that the ended events are not very accurate.请注意, ended的事件不是很准确。 They get triggered on the audio thread and need to travel back to the main thread where they finally get fired when there is some time to do so.它们在音频线程上被触发,需要返回主线程,当有时间时它们最终会被触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单的振荡器,但IOS上的网络音频API没有声音 - Simple oscillator, but no sound with web audio API on IOS Web音频API振荡器的内存泄漏 - Memory leak with web audio api oscillator 振荡器的Web音频FFT? - Web Audio FFT of an Oscillator? Web Audio API Oscillator Node对象生命周期和stop()方法 - Web Audio API Oscillator Node object life span and stop() method 为什么网络音频api振荡器不能在Safari Mobile中播放? - Why won't web audio api oscillator play in Safari Mobile? Web音频振荡器无法创建新的振荡器 - Web Audio Oscillator unable to create new oscillator 如何使用滑块更改鼠标向下/向上触发的振荡器的音调,并在使用Javascript和Web Audio API进行鼠标向上事件后使其保持状态 - How to change pitch of mousedown/up triggered oscillator with slider and have it retain state after mouseup event with Javascript and Web Audio API Web音频振荡器异常响亮? - Web audio oscillator extremely loud? 在使用 Web Audio API 构建的应用程序中,使用 Oscillator.connect() 和 Oscillator.disconnect() 方法打开/关闭声音有多可行? - How feasible is it to use the Oscillator.connect() and Oscillator.disconnect() methods to turn on/off sounds in an app built with the Web Audio API? Web音频振荡器仅在Firefox中单击 - Web audio oscillator is clicking in Firefox only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM