0

I'm trying to load audio with XMLHttpRequests and AudioContext, my code looks like this:

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) ->
                # ...

The error is TypeError: Cannot read property 'decodeAudioData' of undefined.

When I console.log the audioContext I get a valid audioContext object, so why is it undefined on code execution?

1 Answers1

1

It's a problem with bound functions; you can diagnose it by putting a console.log @ inside r.load = (e) -> ....

The solution is to bind r.onload handler using ~>:

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

Check LiveScript docs for Bound Functions.

homam
  • 1,945
  • 1
  • 19
  • 26