简体   繁体   English

网络音频API音量指示器-不再有声音

[英]web audio api volume indicator - no more sound

I try to make a kind of volume meter (as a pulsation effect) with the web audio api from a sound file loaded in an <audio> tag, my indicator effect works fine with this code, I can retrieve volume changes from the playing audio and I use the value to apply opacity effects to a <div> with jquery. 我尝试使用加载在<audio>标签中的声音文件的Web音频api制作一种音量计(作为脉动效果),我的指示器效果在此代码下效果很好,我可以从播放的音频中检索音量变化并且我使用该值将不透明度效果应用于带有jquery的<div>

But I can't ear the sound when I load my script, audio is playing, volume is changing, but no sound. 但是当我加载脚本,播放音频,改变音量,却没有声音时,我听不到声音。 Maybe I missed a connector somewhere? 也许我错过了某个地方的连接器?

Any ideas? 有任何想法吗?

    var audio = document.getElementById('sound');
    var context = new AudioContext();
    var analyser = context.createScriptProcessor(1024, 1, 1);
    var source = context.createMediaElementSource(audio);

    source.connect(analyser);
    analyser.connect(context.destination);

    opacify();

    function opacify(){
        analyser.onaudioprocess = function(e){
            var out = e.outputBuffer.getChannelData(0);
            var int = e.inputBuffer.getChannelData(0);
            var max = 0;
            for(var i = 0; i < int.length; i++){
                out[i] = 0;
                max = int[i] > max ? int[i] : max;
            }
            $('#artist').css({'opacity': max}); // updates opacity from value
        }
    }

Any help appreciated. 任何帮助表示赞赏。 Thanks... 谢谢...

You need to set the output buffer of your scriptNode to something if you want some output from it. 如果要从脚本节点输出一些信息,则需要将其设置为某种值。 By default, the output is empty. 默认情况下,输出为空。

(You can set it to out[i] = int[i] if you want same output). (如果需要相同的输出,可以将其设置为out[i] = int[i] )。

 var audio = new Audio(); audio.crossOrigin = 'anonymous'; audio.src = 'https://dl.dropboxusercontent.com/s/8c9m92u1euqnkaz/GershwinWhiteman-RhapsodyInBluePart1.mp3'; audio.play(); var context = new AudioContext(); var analyser = context.createScriptProcessor(1024, 1, 1); var source = context.createMediaElementSource(audio); source.connect(analyser); analyser.connect(context.destination); opacify(); function opacify() { analyser.onaudioprocess = function(e) { var out = e.outputBuffer.getChannelData(0); var int = e.inputBuffer.getChannelData(0); var max = 0; for (var i = 0; i < int.length; i++) { out[i] = int[i]; // set the output as the input max = int[i] > max ? int[i] : max; } artist.style.opacity = max; } } 
 #artist { height: 50px; width: 50px; background-color: red; } 
 <div id="artist"></div> 

But this will make just unnecessary processing, eating useless memory, so you could even just connect directly the sourceNode to the destinationNode, if you want to ear unprocessed sound from the source. 但这将导致不必要的处理,浪费无用的内存,因此,如果您想从源中听到未处理的声音,甚至可以直接将sourceNode连接到destinationNode。

 var audio = new Audio(); audio.crossOrigin = 'anonymous'; audio.src = 'https://dl.dropboxusercontent.com/s/8c9m92u1euqnkaz/GershwinWhiteman-RhapsodyInBluePart1.mp3'; audio.play(); var context = new AudioContext(); var analyser = context.createScriptProcessor(1024, 1, 1); var source = context.createMediaElementSource(audio); source.connect(analyser); source.connect(context.destination); // connect the source to the destination analyser.connect(context.destination); // chrome needs the analyser to be connected too... opacify(); function opacify() { analyser.onaudioprocess = function(e) { // no need to get the output buffer anymore var int = e.inputBuffer.getChannelData(0); var max = 0; for (var i = 0; i < int.length; i++) { max = int[i] > max ? int[i] : max; } artist.style.opacity = max; } } 
 #artist { height: 50px; width: 50px; background-color: red; } 
 <div id="artist"></div> 

Anyway, out[i] = 0 will do nothing good. 无论如何, out[i] = 0将无济于事。

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

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