I need an audio file from the output of a php script to be placed into the source of a HTML audio tag. I can't specify which as I can't add POST variables so I do so by setting the SESSION "songRequested" through an ajax call and then requesting the file and using the session variable to specify which file is necessary.
HTML
<audio id ="player">
<source src="" id = "source">
</audio>
JS
$.post('includes/songrequest.php',{'request':songName},function(){
$('#source').attr("src","includes/songrequest.php");
player.load();
player.play();
});
//Declare the session variable as the file you want and set the audio source to be the songrequest php below with the session variable as the file to be downloaded.
PHP
<?php
session_start();
if(!isset($_SESSION['u_uid'])){
exit();
}
if(isset($_POST['request'])){
$_SESSION['request'] = $_POST['request'];
//If requesting with post variables, set the session variable to it.
exit();
}
//Otherwise, retrieve the file set as the session variable
$file = dirname(getcwd(),2)."\\users\\".$_SESSION['u_uid']."\\".$_SESSION['
header('Content-Type: audio/'.pathinfo($file,PATHINFO_EXTENSION));
header('Cache-Control: no-cache');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
readfile($file);
I had to go with this as I couldn't pass post variables when I called for a php source in the audio tag and I don't know of a way to really handle audio data natively in javascript.