【问题标题】:AudioContext issue on SafariSafari 上的 AudioContext 问题
【发布时间】:2018-02-13 00:53:12
【问题描述】:

我正在尝试在我的打字稿文件中为 Angular 5 应用程序使用 AudioContext。它在 Chrome 上运行良好,在 Safari 上不起作用。我通过谷歌搜索看到的所有内容都说要使用window.webkitAudioContext,但是当打字稿编译器运行说它在类型 Window 上不存在时,它会立即爆炸。

let context = new AudioContext();
let source = context.createBufferSource();
context
    .decodeAudioData(event.body)
    .then(x => {
        source.buffer = x;
        source.connect(context.destination);
        source.start(0);
    });

【问题讨论】:

    标签: typescript angular5 audiocontext webkitaudiocontext


    【解决方案1】:

    您应该扩展Window 接口,然后扩充窗口对象。

    假设您使用的是 angular-cli,请将以下内容添加到您的 typings.d.ts 文件中:

    declare interface Window {
      AudioContext: AudioContext;
    }
    

    然后在您的polyfills.ts 文件中,您可以设置AudioContext 属性:

    window.AudioContext = window.AudioContext || window.webkitAudioContext;
    

    【讨论】:

      【解决方案2】:

      您可以将window 转换为any

      (window as any).AudioContext = (window as any).AudioContext || (window as any).webkitAudioContext;
      let context = new AudioContext();
      

      【讨论】:

        【解决方案3】:

        您必须将所需的属性添加到全局命名空间中的Window 接口。

        declare global {
          interface Window {
            AudioContext: typeof AudioContext;
            webkitAudioContext: typeof AudioContext;
          }
        }
        
        const context = new (window.AudioContext || window.webkitAudioContext)();
        

        【讨论】:

          【解决方案4】:

          我选择了这条单线:

          const audioContext: AudioContext = new (window["AudioContext"] || window["webkitAudioContext"])();
          

          【讨论】:

          • 这看起来很简单,但不幸的是无法编译:Element implicitly has an 'any' type because index expression is not of type 'number'. TS7015 在表达式中的第二个术语。
          猜你喜欢
          • 1970-01-01
          • 2015-12-10
          • 1970-01-01
          • 2015-07-30
          • 2018-08-29
          • 2019-12-21
          • 2014-10-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多