javascript - Load audio file with PHP and play with JS
966
I am trying to load a wave audio-file with php and play the audio with javascript.
My Javascript code:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'php/test.php', true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4) {
var audio = new Audio("data:audio/wave;base64," + btoa(unescape(encodeURIComponent(xmlhttp.responseText))));
audio.play();
}
}
xmlhttp.send(url);
My PHP code:
<?php
readfile("test.wav");
?>
Unfortunately it's not working. Any ideas?
Answer
Solution:
How about:
And you can just point the AJAX request directly to the
.wav
file.