Im building a webapp using Angular4 that is supposed, among other things, to playback audio files. Unfortunately i have no control over the server serving the media files and therefore can't modify the server settings.
When i now create an audio element and play it directly, the audio playback works as expected:
var audio = new Audio();
audio.src = item.url; // the item is simply an object that holds the url and title
audio.load();
audio.play();
When i now use a player-class that encapsulates the playback functionality i get a CORS error since the mp3 items don't have the appropiate headers set:
@Injectable
export class Player {
audio: any;
constructor(private zone: NgZone){
this.initialize();
}
initialize(){
if (!this.audio) {
this.audio = new Audio();
this.audio.autoplay.false;
this.audio.preload = 'auto';
this.audio.autobuffer = true;
this.audio.crossOrigin = "anonymous";
}
}
setUrl(url: string) {
this.initialize();
this.audio.pause();
this.audio.src = url;
this.audio.load();
}
play() {
this.audio.play();
}
pause() {
this.audio.pause();
}
}
The error message that is returned upon play is the following, which essentially inidactes that the CORS policy prevents the item from being played:
Uncaught (in promise) DOMException: Failed to load because no supported source was found.
Is there any way to work around this? And why does it work when i call the audio element directly in the controller?