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
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
本文标签: How to play sound in javascript without external audio filehtml fileStack Overflow
版权声明:本文标题:How to play sound in javascript without external audio filehtml file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745267431a2650686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论