本文介绍了如何使用网络音频API播放声音文件Safari?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改脚本以播放在Codepen上找到的mp3,以使其在Safari上正常工作.在Firefox和Chrome中,它工作正常,但Safari抱怨:未处理的承诺拒绝:TypeError:参数不足index.html:25"

I'm modifying a script to play an mp3 that I found on Codepen to get it to work on Safari. In Firefox and Chrome it works fine, but Safari complains : "Unhandled Promise Rejection: TypeError: Not enough argumentsindex.html:25"

我已阅读 https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/PlayingandSynthesizingSounds/PlayingandSynthesizingSounds.html 这比我需要的要先进得多.我只想在我的mp3中播放声音.不过,我需要网络音频,因为这是使其在iOS Safari上正常工作的唯一方法.

I've read https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/PlayingandSynthesizingSounds/PlayingandSynthesizingSounds.html That goes into much more advanced stuff than I need. I just want to play the sound in my mp3. I need web audio though, because that is the only way to get it to work on iOS Safari.

有人知道如何使Safari开心吗?

Does anyone know how to make Safari happy?

https://codepen.io/kslstn/full/pagLqL

(function () {

  if('AudioContext' in window || 'webkitAudioContext' in window) {
    // Check for the web audio API. Safari requires the webkit prefix.


  const URL = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/Yodel_Sound_Effect.mp3';

  var AudioContext = window.AudioContext || window.webkitAudioContext;
  var context = new AudioContext(); // Make it crossbrowser

  const playButton = document.querySelector('#play');

  let yodelBuffer;

  window.fetch(URL)
    .then(response => response.arrayBuffer())
    .then(arrayBuffer => context.decodeAudioData(arrayBuffer))
    .then(audioBuffer => {
      yodelBuffer = audioBuffer;
    });

    playButton.onclick = () => play(yodelBuffer);

  function play(audioBuffer) {
    const source = context.createBufferSource();
    source.buffer = audioBuffer;
    source.connect(context.destination);
    source.start();
  }

}

}());
<button id="play">Yodel!</button>

推荐答案

Safari(Webkit)不支持BaseAudioContext.decodeAudioData()的基于Promise的语法.在以下链接中查看详细的浏览器兼容性

The Promise-based syntax for BaseAudioContext.decodeAudioData() is not supported in Safari(Webkit). See detailed browser compatibility in the following link

https://developer.mozilla.org/en-US/docs/Web/API/BaseAudioContext/decodeAudioData

因此,您需要使用以下旧的回调语法.它可以在高于6.0的Safari和高于10的Chrome中使用

So you need to use the old callback syntax as below. It works in both Safari higher than 6.0 and Chrome higher than 10

window.fetch(URL)
  .then(response => response.arrayBuffer())
  .then(arrayBuffer => context.decodeAudioData(arrayBuffer,
                                               audioBuffer => {
                                                 yodelBuffer = audioBuffer;
                                                },
                                               error =>
                                               console.error(error)
                                              ))

这篇关于如何使用网络音频API播放声音文件Safari?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:22