“杂音”的声音时,音频暂停使用Javascript [英] 'Pop noise' sounds when audio paused with Javascript
问题描述
我有一个音频标记在我的HTML,而我已经.WAV里面。使用JavaScript,我选择音频标签和播放WAV,这是我触发使用键盘键。的我想实现,例如,每个A键preSS,重播.WAV /打从一开始的声音)的
音频的播放工作好,所以不会暂停了。不过,我得到一个弹出的噪音,而直接暂停播放.WAV。
VAR音频=的document.getElementById(音);如果(!IsPlaying模块(音频)){
audio.play(); //作品
}其他{
audio.pause(); //弹出此线路上;我在下面评论线检查。
audio.currentTime = 0;
audio.play();
}
我发现这个答案,而据我了解,这是怎么回事,因为我瞬间音量设置为0;但我无法弄清楚我的情况。我相信使用在使用setInterval推子是不是一个好方法。
我也发现了 audio.muted = TRUE
和暂停卷之前使用它试图(和使用 audio.muted = FALSE
打之前的音频),但是这也给杂音
更新:
我想我需要使用淡出来解决这个问题。有没有办法立即淡出音频?
更新:
我想我需要使用淡出来解决这个问题。有没有
办法立刻淡出音频?
块引用>您可以使用
.animate()
到卷
从当前value属性要设置动画0
\r\r\r\rVAR音频= $(音频);\r
$(按钮)。点击(函数(){\r
如果(音频[0] .volume大于0){\r
audio.animate({量:0});\r
//调用`.pause()`这里\r
}\r
});\r
&LT;脚本SRC =https://ajax.googleapis.com/ajax /libs/jquery/2.1.1/jquery.min.js\"></script>\r
&LT;音频控制src=\"https://upload.wikimedia.org/wikipedia/commons/6/6e/Micronesia_National_Anthem.ogg\"></audio>\r
&LT;按钮和GT;淡出音频&LT; /按钮&GT;\r
I have an audio tag in my html, and which I have .wav inside it. With Javascript, I select audio tag and play the wav., which I trigger using a keyboard key. What I am trying to achieve is, for example, on press of each 'A' key, replay the .wav/play the sound from the beginning)
The playing of the audio works okay, and so does the pause too. However, I get a pop noise, while directly pausing the playing .wav.
var audio = document.getElementById(sound); if (!isPlaying(audio)) { audio.play(); // works } else { audio.pause(); // pops on this line; I checked with commenting below lines. audio.currentTime = 0; audio.play(); }
I found this answer, and as far as I understand, it's happening because I instantly set the volume to 0; but I couldn't figure it out for my case. I believe using a fader with setInterval is not a good approach
I also found
audio.muted = true
, and tried using it before pausing the volume (and usedaudio.muted = false
just before playing the audio), but this also gives pop noise
Update:
I think I need to use fade out to work around this issue. Is there a way to fade out audio instantly?
解决方案Update:
I think I need to use fade out to work around this issue. Is there a way to fade out audio instantly?
You can use
.animate()
to animatevolume
property from current value to0
var audio = $("audio"); $("button").click(function() { if (audio[0].volume > 0) { audio.animate({volume:0}); // call `.pause()` here } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <audio controls src="https://upload.wikimedia.org/wikipedia/commons/6/6e/Micronesia_National_Anthem.ogg"></audio> <button>fade out audio</button>
这篇关于“杂音”的声音时,音频暂停使用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!