javascript - 使用jQuery setInterval后音频元素音量未重置

标签 javascript jquery audio setinterval

下面的代码随机选择并播放(由用户选择的)不同音高的音频元素。我正在研究淡入淡出选项;用户指定以秒为单位的淡入时间,该淡入时间由ID(fade.value)传递。

我实际上有两个问题:
A)在第一次迭代中,音频元素在开始播放之前就开始褪色。
B)重复调用同一元素时,如果淡入时间长于重复时间,则无法正确重置音量(这是tone.volume=1的预期点),但会“保持下降”。

function pickAndPlay(pitchSet, saturation){
    var pitchCheck = rand(1,100);
    if (pitchCheck <= saturation){
        fyshuffle (pitchSet); // the Fischer-Yates shuffle
        var tone = document.getElementById(pitchSet[0]);
        tone.currentTime = 0;
        tone.volume = 1;
        tone.play();
        if(fadeTime.value>0){
            $(tone).animate({volume: 0}, fadeTime.value*1000);

        };
        document.getElementById("noteName").value=pitchSet[0];
        console.log(iteration);
        iteration++;
        console.log("tone.volume:"+tone.volume);
    };
};
function nextThing(millis,pitchSet,saturation){  
    if(iteration==0){pickAndPlay(pitchSet,saturation)};
    return setInterval(pickAndPlay,millis,pitchSet,saturation);  // this needs `return`: https://stackoverflow.com/questions/44609995/settimeout-recursion-javascript
};

感谢您提供任何有关解决这些问题的建议或引用。

最佳答案

对于第二个问题,当音频再次开始播放时,动画仍在运行。只需使用jQuery stop方法:

function pickAndPlay(pitchSet, saturation){
    var pitchCheck = rand(1,100);
    if (pitchCheck <= saturation){
        fyshuffle (pitchSet);
        var tone = document.getElementById(pitchSet[0]);
        tone.currentTime = 0;
        $(tone).stop();
        tone.volume = 1;
        tone.play();
        if(fadeTime.value>0){
            $(tone).animate({volume: 0}, fadeTime.value*1000); console.log(fadeTime.value);
        };
        document.getElementById("noteName").value=pitchSet[0];
        iteration++;
    };
};

关于javascript - 使用jQuery setInterval后音频元素音量未重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44748798/

相关文章:

javascript - 我试图在 Dreamweaver 中包含 JQuery,但它没有被识别。不是文件,不是来自 CDN

javascript - 在 y 轴上添加点 HIGHCHARTS

ios - 使用 YouTube Helper Library iOS 在后台播放 youtube 视频

javascript - 我怎样才能检测到我的 Node.js 脚本被永远终止了?

javascript - 带数组的循环不适用于 innerHTML

jquery - jQuery 没有使用 name 属性的选择器吗?如果是的话为什么?有什么可能的解决办法?

javascript - JQuery - 将逗号添加到数字输入但不提交

audio - 现代声卡通常包含模拟 OPL2 (YM3812) 芯片吗?

ios - AudioQueueDispose延迟

javascript - TypeScript 在静态上下文中从类创建新实例