【问题标题】:AudioContext Analyser working on firefox/chrome but not SafariAudioContext Analyzer 在 firefox/chrome 上工作,但在 Safari 上不工作
【发布时间】:2016-02-10 11:16:13
【问题描述】:

我正在为音频编写可视化工具,但在 mac os x 上的 safari 以及使用远程调试器进行 ios 调试时遇到问题。在更新 AudioContext 分析器的动画函数上,ByteFrequency 数组中的值不会在 safari 上更新。以下是代码示例:

var context;
if (typeof AudioContext !== "undefined") {
    context = new AudioContext();
} else if (typeof webkitAudioContext !== "undefined") {
    context = new webkitAudioContext();
}
var analyser = context.createAnalyser();
analyser.fftSize = 64;
frequencyData = new Uint8Array(analyser.frequencyBinCount);

// Get the frequency data and update the visualisation

function update() {
    requestAnimationFrame(update);
    analyser.getByteFrequencyData(frequencyData); 
};

$(document).ready(function(){
    $("#player").bind('canplay', function() {
        var source = context.createMediaElementSource(this);
        source.connect(analyser);
        analyser.connect(context.destination);
    });
};

这里是工作示例http://basketballjock.org/的链接

【问题讨论】:

    标签: javascript webkit requestanimationframe audiocontext webkitaudiocontext


    【解决方案1】:

    在 Safari 中,您需要在用户交互后创建音频上下文。例如在按钮 onclick 回调中。

    HTML: <button onclick="play">Play</button>

    Javascript:

    function play() {
      var context;
      if (typeof AudioContext !== "undefined") {
          context = new AudioContext();
      } else if (typeof webkitAudioContext !== "undefined") {
          context = new webkitAudioContext();
      }
      var analyser = context.createAnalyser();
      analyser.fftSize = 64;
      frequencyData = new Uint8Array(analyser.frequencyBinCount);
    
      // Get the frequency data and update the visualisation
    
      function update() {
        requestAnimationFrame(update);
        analyser.getByteFrequencyData(frequencyData); 
      };
    
      var source = context.createMediaElementSource(this);
      source.connect(analyser);
      analyser.connect(context.destination);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      相关资源
      最近更新 更多