简体   繁体   English

不带按钮的HTML5静音/取消静音

[英]Mute/Unmute Sound w/o Button HTML5

Problem: Finding an efficient function for Muting and Unmuting Audio (w/o VideoPlayer) in HTML5 automatically until a defined time is past. 问题:自动找到一种有效的功能,可以自动将HTML5中的音频(不带VideoPlayer) 静音和取消静音,直到经过定义的时间为止。

I set the gain of an oscillator to increase gradually from 0 to 1 (interrupted by a bunch of 1second of silence in each unit of gain's increase) until 30s are reached. 我将振荡器的增益设置为从0逐渐增加到1(在每个增益增加单位中被一秒钟的静音间隔打断)直到达到30s。 For this purpose the setValueAtTime function (I added increments to both values of gain and time) was called. 为此,调用了setValueAtTime函数(我将增量添加到了增益和时间两个值)。

It doesn't seem efficient, however, because the iterations don't follow an accurate real-life-corresponding time, even if the arguments are set individually to change values in a particular time -> this demanded a dummy effort to test. 但是,这似乎并不有效,因为即使参数被单独设置为在特定时间内更改值,迭代也不会遵循与实际生活相对应的准确时间,这需要花费大量精力进行测试。 More interesting would be setValueCurveAtTime and setLinearRampAtTime , but they don't seem to allow a distribution of "silences" during the increasing tone. 更有趣的是setValueCurveAtTimesetLinearRampAtTime ,但是它们似乎不允许在渐增的色调期间分配“沉默”。

The only references found regarded UserInteraction with a custom button, which is not automatic; 找到的唯一参考将UserInteraction与自定义按钮结合使用,该按钮不是自动的。 and JQuery functions. 和JQuery函数。 I found no documentation precise enough. 我发现没有足够精确的文档。 The function "document.getElementById(id).setVolume(0);" 函数“ document.getElementById(id).setVolume(0);” for example doesn't offer neither an option of defining the duration of the silence, nor the option to insert this 1s of silence homogeneously along the 30seconds of an increasing tone. 例如,既没有提供定义静默持续时间的选项,也没有提供沿逐渐增加的30秒均匀插入此1s静默的选项。

Once the function is defined, I would call setTimeout so that it played immediately and then setInterval ("function()", 30000). 定义函数后,我将调用setTimeout使其立即播放,然后调用setInterval (“ function()”,30000)。

Snippet of code 程式码片段

context=new AudioContext();
var oscillator = context.createOscillator();
var gainR = context.createGain(); 
oscillator.connect(gainR);
gainR.connect(context.destination);
gainR.gain.setValueAtTime(0,currenttime);
loudnessup = function (){
var c = currenttime;
var Loud=0.2; 
gainR.gain.setValueAtTime(Loud, c+2);
gainR.gain.setValueAtTime(0, c+3);
gainR.gain.setValueAtTime(Loud+0.05, c+3.5);
gainR.gain.setValueAtTime(0, c+4.5);
//...continues iterations
gainR.gain.setValueAtTime(Loud+0.8, c+28);
gainR.gain.setValueAtTime(0, c+30);};
setTimeout("loudnessup()",currenttime); 
setInterval(function(){
n=n+1; //after 30s the freq value should arise accordingly to an array 
oscillator.frequency.value = frequencies[n];},30000);}

After the 30s, the loop w/ a higher freq doesn't seem to go any further 30年代后,带有较高频率的循环似乎不再继续

Here is an example using the built-in high-resolution timer to set the volume over a short segment relative to the start time. 这是使用内置高分辨率计时器设置相对于开始时间的一小段音量的示例。

It will create two events for each segment to ramp volume quickly. 它将为每个分段创建两个事件以快速提高音量。 Each segment is toggled so the first time it will ramp up the volume, the second time ramp it down. 每个段都被切换,因此第一次将音量增大,第二次将音量减小。 Using a slight ramp will also prevent clicks in the sound. 略微倾斜也会防止声音滴答声。

Using a toggle state you can create two events defining a short ramp for each segment. 使用切换状态,您可以创建两个事件,为每个段定义一个短斜坡。 If the toggle is true if will go from current volume to 0, otherwise from 0 to current volume: 如果切换为true,则将从当前音量变为0,否则从0变为当前音量:

var toggle = true;
var now = actx.currentTime;
...

for(var i = 0; i < duration; i += step) {
    var volume = i / duration;

    g.gain.linearRampToValueAtTime(toggle ? volume : 0, now + i);
    g.gain.linearRampToValueAtTime(toggle ? 0 : volume, now + i + 0.012);
    toggle = !toggle;
}

Live example 现场例子

 document.querySelector("button").onclick = function() { this.style.display = "none"; playSound(); // first play setInterval(playSound, 30000); // next play, after and every 30 sec. }; function playSound() { var duration = 30, step = 1, toggle = false, actx = new (AudioContext || webkitAudioContext)(), osc = actx.createOscillator(), g = actx.createGain(); osc.frequency.value = 440; g.gain.value = 0; osc.connect(g); g.connect(actx.destination); var now = actx.currentTime; for(var i = 0; i < duration; i += step) { // volume for this segment when toggled on var volume = i / duration; // create a short ramp using two events 12ms appart g.gain.linearRampToValueAtTime(toggle ? volume : 0, now + i); g.gain.linearRampToValueAtTime(toggle ? 0 : volume, now + i + 0.012); toggle = !toggle; } osc.start(); osc.stop(now + duration); }; 
 button {font:bold 20px sans-serif} 
 <button>Start</button> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM