我创建了代码来使用网络音频 api 生成莫尔斯电码声音。摩尔斯电码声音工作完美。我想用那个声音来闪烁屏幕的一部分。只有两个声音点(。)和破折号(-)。我想通过闪烁屏幕的一部分来显示消息。 我尝试将 div 的背景颜色设置为黑色,然后隐藏/显示该 div 以提供闪光效果。但它没有按预期工作。请帮助我....提前谢谢...我试过这个:
$(document).ready(function() {
var context = new (window.AudioContext || window.webkitAudioContext());
var O= new MorseNode(context,20);
O.connect(context.destination);
O.playString(1,'.-- -..');
});
function MorseNode(ac, rate) {
// ac is an audio context.
this._oscillator = ac.createOscillator();
this._gain = ac.createGain();
this._gain.gain.value = 0;
this._oscillator.frequency.value = 550;
this._oscillator.connect(this._gain);
if(rate == undefined)
rate = 20;
this._dot = 1.2 / rate; // formula from Wikipedia.
this._oscillator.start(0);
}
MorseNode.prototype.connect = function(target) {
return this._gain.connect(target);
}
MorseNode.prototype.playChar = function(t, c) {
for(var i = 0; i < c.length; i++) {
switch(c[i]) {
case '.':
$('#flashBlock').hide(); //I tried this to flash the screen.
this._gain.gain.setValueAtTime(1.0, t);
t += this._dot;
this._gain.gain.setValueAtTime(0.0, t);
$('#flashBlock').show();
break;
case '-':
$('#flashBlock').hide();
this._gain.gain.setValueAtTime(1.0, t);
t += 3 * this._dot;
this._gain.gain.setValueAtTime(0.0, t);
$('#flashBlock').show();
break;
}
t += this._dot;
}
return t;
}
MorseNode.prototype.playString = function(t, w) {
w = w.toUpperCase();
for(var i = 0; i < w.length; i++) {
if(w[i] == ' ') {
t += 3 * this._dot; // 3 dots from before, three here, and
// 1 from the ending letter before.
}
else if(w[i] != undefined) {
t = this.playChar(t, w[i]);
t += 2 * this._dot;
}
}
return t;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<html>
<div id="flashBlock" style="Background:black;display:none;height:100px;width:100px">
</div>
</html>