我有一个 Angular 5 应用程序,我在其中设置了click
一个按钮的处理程序来下载音频文件并播放它。我正在使用此代码来执行此操作:
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);
});
}
如果我转到 Chrome 中的页面并按下按钮,音频会立即启动。如果我在 Safari 中执行此操作,则不会发生任何事情。我知道 Safari 锁定了一些东西,但这是对按钮单击的响应,它不是自动播放。
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');