Tell me, please, is it possible to put the reader.result in const audio = new Audio("");, namely in ""? If so, please tell me how to do it. Full code:
function previewWAV() {
var preview = document.querySelector('.download-button.fas.fa-download');
var previewDisplay = document.querySelector('.preview-div.wav');
var file = document.querySelector('#upload').files[0];
var reader = new FileReader();
previewDisplay.style.display = "block";
reader.onloadend = function () { preview.href = reader.result; const audio = new Audio("reader.result"); }
if (file) { reader.readAsDataURL(file); } else { preview.src = ""; } }
That won't work.
Audio() accepts a URL as its sole parameter.
FileReader.result returns the contents of the file, not its URL.
If the file you're trying to play is already at a URL you can just feed that URL to Audio() and skip the whole FileReader step.
If you're trying to preview a file before upload, you can use URL.createObjectURL() on the file object to generate a usable DOMString you can pass to Audio():
let file = document.querySelector('#upload').files[0];
let fileURL = URL.createObjectURL(file);
let audio = new Audio(fileURL);