How to play sound in javascript without external audio filehtml file? - Stack Overflow

So im building something but im not allowed to use any external files other than the script.js file its

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745267432a4619541.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信