nael_d
nael_d

Reputation: 27

How to stream audio file from another domain using jquery ajax avoiding crossdomain policy?

I'm building a web audio player like SoundCloud footer player using jQuery and HTML5 audio tag. The only-one problem is how to stream an audio file from another domain? (This player plugin could be installed in any website with some basic setup in JSON).

This is the console response when trying to stream an audio file:

Console response

The expected answers should be in jQuery.

Update: I've tried to use 'crossdomain' html5 attribute and crossDomain property in jQuery.ajax() but no effect.

Thanks a lot.

Upvotes: 0

Views: 249

Answers (1)

Brad
Brad

Reputation: 163303

A couple routes...

Firstly, if you're not reading that audio data or doing anything client-side with that audio, you can just use <audio src="..." /> as normal. CORS doesn't come into play until you're actually trying to use script to read that audio. If you're using some sort of special player code... don't. Just use a normal HTML5 <audio> tag or the Audio class.

Next, if you do need to read the raw audio data, either direct from the URL or to record/process the resulting decoded audio, your server must be configured to allow for CORS for this resource. Access-Control-Allow-Origin: * in the response headers is the easiest way to get there, but you should read up on CORS and understand it before proceeding so that you don't accidentally make something insecure on that server.

Upvotes: 1

Related Questions