【问题标题】:AudioContext not working on Safari but working on Google ChromeAudioContext 无法在 Safari 上运行,但在 Google Chrome 上运行
【发布时间】:2022-01-07 22:55:52
【问题描述】:

我知道我需要在 Safari 中进行用户交互后创建音频上下文。但是,下面的代码还是不行。

HTML

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

Javascript

function play() {  
  var AudioContext = window.AudioContext || window.webkitAudioContext
  var ctx = new AudioContext()

  fetch('MP3_URL')
    .then((response) => response.arrayBuffer())
    .then((arrayBuffer) => ctx.decodeAudioData(arrayBuffer))
    .then((audioBuffer) => {
      var source = ctx.createBufferSource()
      source.buffer = audioBuffer
      source.connect(ctx.destination)
      source.start(ctx.currentTime)
    })
}

在谷歌浏览器 bug safari 上运行相同的代码。

我在这里做错了什么吗?非常感谢!!

【问题讨论】:

    标签: javascript html safari audiocontext


    【解决方案1】:

    对我来说真的没有意义,但在这种情况下,Safari 需要再次推动才能启动AudioContext。在创建 AudioContext 后立即调用 resume() 应该可以工作。

    // ...
    var ctx = new AudioContext();
    
    ctx.resume();
    
    fetch('MP3_URL')
    // ...
    

    【讨论】:

      猜你喜欢
      • 2013-07-08
      • 2018-02-04
      • 2017-11-13
      • 1970-01-01
      • 2011-05-13
      • 2017-02-26
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      相关资源
      最近更新 更多