I am trying to load some audio into Playcanvas from an mp3 file on Hygraph CMS. I have the URL for the file, so I have been trying a function like this:
ContentLoader.prototype.loadAudioFromURL = async function(slot) {
this.app.assets.loadFromUrl("https://media.graphassets.com/hUKbpBVTvCfhWOXi3eRC", 'audio', function (error, asset) {
if (error) {
console.log(error);
return;
}
slot.audio = asset.resource;
}.bind(slot));
};
However, I get the following error:
Error loading audio url: hUKbpBVTvCfhWOXi3eRC: Audio format for hUKbpBVTvCfhWOXi3eRC not supported
Even though it is an mp3, I’m guessing that maybe because it doesn’t have ‘/mp3’ on the end it might be causing this issue?
So instead, I am trying to load it in using the Web Audio API, and I’m not getting errors with the following code, it’s just not actually playing so I don’t know if I’m missing something obvious here…
ContentLoader.prototype.loadAudio = async function() {
this.context = this.app.systems.sound.context;
this.audio = this.context.createGain();
const resp = await fetch("https://media.graphassets.com/hUKbpBVTvCfhWOXi3eRC");
const arrayBuffer = await resp.arrayBuffer();
const audioBuffer = await this.context.decodeAudioData(arrayBuffer);
const source = this.context.createBufferSource();
source.loop = false;
source.buffer = audioBuffer;
source.connect(this.audio);
source.start();
};
What am I doing wrong?