繁体   English   中英

AudioContext 不适用于 Safari 但适用于 Google Chrome

[英]AudioContext not working on Safari but working on Google Chrome

我知道我需要在 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)
    })
}

在 Google Chrome 错误 safari 上工作的相同代码。

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

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

// ...
var ctx = new AudioContext();

ctx.resume();

fetch('MP3_URL')
// ...

暂无
暂无

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

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