remove createMediaElementSource

情到浓时终转凉″ 提交于 2019-12-05 17:58:26

I've faced the same issue. Unfortunately I didn't find how to get already created MediaElementSourceNode from an audio element. Nevertheless it's possible to workaround this issue by using WeakMap to remember MediaElementSourceNode:

var MEDIA_ELEMENT_NODES = new WeakMap();

function analyzerInitialize() {
  if (context == undefined) {
    context = new AudioContext();
  }
  analyser = context.createAnalyser();
  canvas = analyserElement;
  ctx = canvas.getContext('2d');
  if (MEDIA_ELEMENT_NODES.has(audio)) {
    source = MEDIA_ELEMENT_NODES.get(audio);
  } else {
    source = context.createMediaElementSource(audio);
    MEDIA_ELEMENT_NODES.set(audio, source);
  }
  source.connect(analyser);
  analyser.connect(context.destination);
  frameLooper();
}

By using WeakMap I avoid memory issues.

You can set the context and source to the globally defined variable instead of redefining the variables

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