Chrome - createMediaElementSource not working

点点圈 提交于 2019-12-11 00:43:52

问题


I have a simple audio analyzer for my website:

var analyser = document.getElementById('analyzer');
var beat = document.getElementById('track_meta')
var canvas, ctx, source, context, analyser, fbc_array, bars, bar_x,
bar_width, bar_height;

function playAnalyzer() {
    context = new AudioContext();
    analyser = context.createAnalyser();
    canvas = document.getElementById('analyzer');
    ctx = canvas.getContext('2d');
    source = context.createMediaElementSource(playerE);
    source.connect(analyser);
    analyser.connect(context.destination);
    frameLooper();
}

function frameLooper() {
    canvas.width  = canwidth;
    canvas.height = canheight;
    ctx.imageSmoothingEnabled = false;
    window.requestAnimationFrame(frameLooper);
    fbc_array = new Uint8Array(analyser.frequencyBinCount);
    analyser.getByteFrequencyData(fbc_array);
    ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
    ctx.fillStyle = "white"; // Color of the bars
    function valBetween(v, min, max) {
    return (Math.min(max, Math.max(min, v)));
    }
    var beatc = fbc_array[2] / 4;
    var beatround = Math.round(beatc);
    //if (beatround < 10) {
    //    ctx.globalAlpha = '0.1125';
    //}
    //else {
    //    ctx.globalAlpha = '0.' + beatround;
    //}
    bars = canbars;
    for (var i = 0; i < bars; i += canmultiplier) {
        bar_x = i * canspace;
        bar_width = 2;
        bar_height = -3 - (fbc_array[i] / 2);
        ctx.fillRect(bar_x, canvas.height, bar_width, bar_height);
    }
}

My issue is that this does not work unless I am in incognito mode on chrome, otherwise I get this error:

Uncaught InvalidStateError: Failed to execute 'createMediaElementSource' on 'AudioContext': HTMLMediaElement already connected previously to a different MediaElementSourceNode.

I've tested this in Edge, Firefox and Opera and there it works fine. I've even reinstalled chrome to see if that would fix the issue to no avail. I just don't understand why it works fine in incognito mode but not otherwise.

So my question is, is it possible instead of using the <audio> element, connect the analyzer to a var audio = new Audio instead? If so, how would I do this?


回答1:


Do you possibly have a Chrome extension installed that is inserting AudioContexts and creating MediaElementSources? Try disabling your extensions one at a time.




回答2:


So for me, this worked:

Instead of using the <audio> element, use HTMLMediaElement using javascript. And then the only thing you need to do in order to connect the analyzer to your var audio = new Audio() is simply reference your audio variable in the source = context.createMediaElementSource(audio); variable.

This also fixed my errors.



来源:https://stackoverflow.com/questions/35303362/chrome-createmediaelementsource-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!