Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MediaElementWebAudio CORS requests #2014

Closed
sundayz opened this issue Jul 21, 2020 · 1 comment
Closed

MediaElementWebAudio CORS requests #2014

sundayz opened this issue Jul 21, 2020 · 1 comment

Comments

@sundayz
Copy link
Contributor

sundayz commented Jul 21, 2020

Wavesurfer.js version(s): 3.3.3

Browser and operating system version(s): Windows Firefox, Chrome

When using MediaElementWebAudio and providing my own peaks data, the backend doesn't load the mp3 file. Firefox gives the error: The HTMLMediaElement passed to createMediaElementSource has a cross-origin resource, the node will output silence. and Chrome: MediaElementAudioSource outputs zeroes due to CORS access restrictions

My web server is sending the cors headers:
Access-Control-Allow-Origin "*"
Access-Control-Allow-Methods "GET"
Access-Control-Allow-Headers "Content-Type: audio/mpeg"

I tried changing xhr settings in wavesurfer too but it didn't help.

After some googling, every answer seems to tell me to do this:
audioElement.crossOrigin = 'anonymous';

source: https://stackoverflow.com/questions/41069039/cors-request-will-not-work-in-chrome

Would changing createMediaElementSource here https://github.com/katspaugh/wavesurfer.js/blob/master/src/mediaelement-webaudio.js#L56 to allow passing an option param be a good idea?

mediaElement.crossOrigin = params.CrossOrigin || "";
@sundayz
Copy link
Contributor Author

sundayz commented Jul 21, 2020

Okay so I actually looked a bit closer and it turns out that when you call wavesurfer.load() you can provide your own Audio object instead of a String and wavesurfer uses that. So you can do :

const myAudio = new Audio( your url to mp3 );
myAudio.crossOrigin = 'anonymous';
wavesurfer.load(myAudio, peaks);

And that fixes the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant