admin管理员组

文章数量:1417442

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
Add a ment  | 

2 Answers 2

Reset to default 7

You 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

本文标签: How to play sound in javascript without external audio filehtml fileStack Overflow