So im building something but im not allowed to use any external files other than the script.js file itself. I want to play a .mp3 sound in a function but im not sure how to do it without uploading the file into my folder.
So im building something but im not allowed to use any external files other than the script.js file itself. I want to play a .mp3 sound in a function but im not sure how to do it without uploading the file into my folder.
Share Improve this question asked Aug 14, 2021 at 14:06 LettyLetty 1071 silver badge6 bronze badges 2- How do you expect to play audio when there's no audio ? - do you have it's buffer ? – JS_INF Commented Aug 14, 2021 at 14:28
- Does this answer your question? Can audio files be used inline in HTML? – AaronJ Commented Aug 14, 2021 at 14:29
2 Answers
Reset to default 7You can use javascript to set the audio data src to data:audio/ogg;base64,T2dnUwACAAAAAAAAAAA+...
etc.
There's an example here. https://iandevlin./html5/data-uri/audio.php
To do this with javascript simply
var audioElement = new Audio();
audioElement.src = "data:audio/ogg;base64,T2dnUwACAAAAAAAAAAA+...";
audioElement.play();
You can encode your mp3 file using base64. Than you can the audio in a string:
var beep = "data:audio/mp3;base64,<paste your base64 here>";
var audio = document.getElementById('audio');
audio.src = beep;
audio.play();
The base64 string can be generate using a shell
cat sound.mp3 | base64