I have been trying to build a site that plays audio files that are stored remotely (and are not publicly visible). The way that I have been playing audio is:
- Get the audio from server via XMLHttpRequest
- Store this audio in a blob
- Play the audio using Howler.js
I accomplish this with the following code:
//Get the audio file from the API
var request = new XMLHttpRequest();
request.addEventListener("load", loadMusic);
request.responseType = "blob";
request.open("GET", root+"getmusic/"+path);
request.send();
//Function that catches the data, and plays the audio
function loadMusic(data) {
var objectUrl = window.URL.createObjectURL(data.currentTarget.response);
var howler = new Howl({
autoplay: true,
src: [objectUrl],
format: ["mp3"]
});
howler.play();
}
This works fine on my desktop browsers Chrome, Safari, and Firefox (audio plays). However, when open the page on my phone (iPhone 6s, iOS) in either iOS Safari, or the mobile version of Chrome, the audio does not play, and I don't get any errors in the development console to give me a clue as to why. What puzzles me most is why it works in my desktop browser and not in a mobile environment.
Is there any way to fix this? Or debugging routes I should pursue?
Some extra information:
- The user needs to press a button to load/start the audio, so they do interact with the DOM first.
- Audio files are in mp3 format.