【问题标题】:Livescript: AudioContext can't get definedLivescript:无法定义 AudioContext
【发布时间】:2015-09-04 01:20:37
【问题描述】:

我正在尝试使用 XMLHttpRequests 和 AudioContext 加载音频,我的代码如下所示:

class AudioExample
    audioContext: null
    init: ->
        AudioContext = window.AudioContext || window.webkitAudioContext
        @audioContext = new AudioContext!
        # r = xmlhttprequest magic
        r.onload = (e) ->
            rr = e.target #XMLHttpRequest{response: ArrayBuffer, song: Object, si: Object}
            @audioContext.decodeAudioData rr.response, (buffer) ->
                # ...

错误是TypeError: Cannot read property 'decodeAudioData' of undefined.

当我对 audioContext 进行 console.log 时,我得到了一个有效的 audioContext 对象,那么为什么它在代码执行时未定义?

【问题讨论】:

    标签: javascript livescript webkitaudiocontext audiocontext


    【解决方案1】:

    这是绑定函数的问题;您可以通过在r.load = (e) -> ... 中添加console.log @ 来诊断它。

    解决方案是使用~>绑定r.onload处理程序:

    r.onload = (e) ~> ...
    

    检查 LiveScript 文档以获取 Bound Functions

    【讨论】:

      猜你喜欢
      • 2013-09-11
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多