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.
Asked
Active
Viewed 1,392 times
1
-
How do you expect to play audio when there's no audio ? - do you have it's buffer ? – JS_INF Aug 14 '21 at 14:28
-
Does this answer your question? [Can audio files be used inline in HTML?](https://stackoverflow.com/questions/61303710/can-audio-files-be-used-inline-in-html) – AaronJ Aug 14 '21 at 14:29
2 Answers
3
You can use javascript to set the audio data src to data:audio/ogg;base64,T2dnUwACAAAAAAAAAAA+...
etc.
There's an example here. https://iandevlin.com/html5/data-uri/audio.php
To do this with javascript simply
var audioElement = new Audio();
audioElement.src = "data:audio/ogg;base64,T2dnUwACAAAAAAAAAAA+...";
audioElement.play();

AaronJ
- 1,060
- 10
- 24
1
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

Markus
- 1,909
- 4
- 26
- 54