I've got an angular 5 application where I've set the click
handler of a button to download an audio file and play it. I'm using this code to do so:
onPreviewPressed(media: Media): void {
const url = ".....";
this.httpClient.get(url, {responseType: 'blob'}).subscribe(x => {
const fileReader = new FileReader();
fileReader.onloadend = () => {
const context = new ((<any>window).AudioContext || (<any>window).webkitAudioContext)();
const source = context.createBufferSource();
context.decodeAudioData(fileReader.result, buffer => {
source.buffer = buffer;
source.connect(context.destination);
source.start(0);
}, y => {
console.info("Error: " + y);
});
};
fileReader.readAsArrayBuffer(x);
});
}
If I go to the page in Chrome and press the button the audio starts right up. If I do it in Safari nothing happens. I know Safari locked things down but this is in response to a button click, it's not an auto-play.
The audio is sent back from the server via a PHP script, and it's sending headers like this, in case it matters:
header("Content-Type: audio/mpeg");
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($_GET['file']));
header('Cache-Control: no-cache');