我试图用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.
当我console.log audioContext时,我得到了一个有效的audioContext对象,那么为什么在执行代码时没有定义它呢?
发布于 2015-09-07 16:03:47
这是绑定函数的一个问题;您可以通过将console.log @
放入r.load = (e) -> ...
来诊断它。
解决方案是使用r.onload
绑定~>
处理程序。
r.onload = (e) ~> ...
检查LiveScript文档中的界函数。
https://stackoverflow.com/questions/32388717
复制相似问题