question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

undefined is not a constructor (evaluating 'new window.AudioContext') on Safari

See original GitHub issue

the error is TypeError: undefined is not a constructor (evaluating 'new window.AudioContext')

Steps to Reproduce

I reproduced using a fresh clone of the example-ts-webpack, and tested with both 0.21.0 and 0.22.0 versions of excalibur. Start the running the project and press the play button and error will appear and the game will not start.

More specifically the error comes from calling ex.AudioContextFactory.create(); the create function does not properly set the audio context if the case is webkit.

Expected Result

Expect for the audio context to be defined or gracefully fail

Actual Result

Error is thrown and execution halts.

Environment

  • browsers and versions: Safari 12.1 (14607.1.40.1.4)
  • operating system: OSX
  • Excalibur versions: 0.21.0, 0.22.0

Current Workaround

Its possible to redefine the static create method to return the webkitAudioContext

ex.AudioContextFactory.create = () => new (<any>window).webkitAudioContext();

which would allow at least for the game to load.

Possible Solution

possible fix might be something like:

public static create(): AudioContext {
    if (!this._INSTANCE) {
      if ((<any>window).AudioContext) {
        this._INSTANCE = new (<any>window).AudioContext();
      } else if (<any>window).webkitAudioContext) {
         this._INSTANCE = new (<any>window).webkitAudioContext();
      }
    }
    return this._INSTANCE;
  }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
magnusandycommented, Apr 20, 2019

Interesting, I didn’t even think of the build process. I tried messing around here with the webpack config to try get the polyfill to be applied earlier without much success (but I am not too familiar with webpack). I think your proposed code and solution makes more sense in the long run, as it doesn’t rely on tweaking the build process to work properly.

Thanks for the speedy response! I have really been enjoying building with Excalibur 😄

0reactions
eonarheimcommented, Apr 20, 2019

Minification seems to have re-ordered the definition of the polyfill. We can address this in 2 ways, export a polyfill applier function that can be called (currently it’s a side effecting import in Engine.ts), update webpack configuration to produce the correct ordering.

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

AudioContext on Safari - Stack Overflow
AudioContext on Safari · I follow the first example on HTML5Rocks and I get ''Web Audio API is not supported in this browser'....
Read more >
AudioContext() - Web APIs - MDN Web Docs - Mozilla
The AudioContext() constructor creates a new AudioContext object which represents an audio-processing graph, built from audio modules linked ...
Read more >
Javascript – AudioContext on Safari – iTecNote
AudioContext || window.webkitAudioContext); // TypeError: 'undefined' is not a constructor (evaluating 'new(window.AudioContext || window.
Read more >
createMediaElementSource() not wor… - Apple Developer
We're using new webkitAudioContext() in Safari 15.0 on MacBook and iOS Safari on iPhone and iPad to create AudioContext instance, and we're creating ......
Read more >
Web Audio API - W3C
const context = new AudioContext (); function playSound () { const source ... [ Exposed = Window ] interface AudioBuffer { constructor ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found