I am trying to use the Web Audio API
to play sound in my React
application.
It's currently playing sound in all browsers except Safari v12.1
.
I am aware Safari has restrictions on autoplay and requires user interaction to play sound, so I have a play button which calls the _play()
function:
_play = (url, index) => {
this._getData(url);
this.source.start(index)
}
It's calling the _getData()
function which looks like this:
_getData(url) {
this.source = this.audioContext.createBufferSource();
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = () => {
var audioData = request.response;
console.log(this.audioContext)
this.audioContext.decodeAudioData(audioData, buffer => {
this.source.buffer = buffer;
this.source.connect(this.audioContext.destination);
},
function(e){ console.log("Error with decoding audio data" + e.err); });
}
request.send();
}
this.audioContext
is created in the component constructor
using:
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
The console.log(this.audioContext)
inside the request.onload
outputs this before pressing play:
...and this after pressing play:
But no sound is playing (in Safari).
What am I doing wrong?