I used ajax to fetch a mp3 file from the server as a string by using "file_get_contents" method. Now i want to convert that string back to actual sound so that it can be played using js. I'm a bit new to php. Any help would be appreciated. The exact lines of my php code are -
<?php
$file = "txa.mp3";
$fx1 = fopen($file, "rb");
$fx2 = file_get_contents($file);
echo $fx2;
exit();
I want to convert the string back into original sound using Javascript or any of its implementations or if anyone has any better way of streaming audio without letting users know the actual path of the file then let me know.
You will need to pass that file content to html5 audio tag, and attribute src
Kind of
<audio controls src="data:audio/mp3;base64,HERE YOUR CONTENT OF $fx2">
And if you want to use javascript for this reason you might use DOM selectors to set that value to attribute src.
Related
I have a voice recorder in Javascript, and the recorded output is accessed through its url in php and passed into a python file to be used by the speech_recognition library for a text conversion like so:
$aud = $_FILES['audio_data']['tmp_name'];
$output = shell_exec('python ImageToText.py '. $aud.' 2>&1');
But I get this error:
ValueError: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format
Some research revealed that it's because the recording is in webm format, and further research revealed that I can use sox to convert audio from one form to another. I wanted to know if there was a way to do this in php to pass a WAV file to the python file or if there was any other way to do this. Thank you.
I have the contents of an mp3 file saved as a variable, (this isn't meant to be practical, just for a little project of mine), and I wish to play the contents of this variable as an audio file. This would be a simple task with something like node, but unfortunately I must do this entirely client side.
Please note I can not just save the content of the string as an mp3 file, I need to be able to play it from a variable.
I have looked into this, but from what I have found, it appears that this can not be done. If any of you have a solution, I would appreciate hearing it.
This is not very practical, as you're going to get very high memory footprints within the JS engine and will likely cause unnecessary garbage collection... but it is possible to a base64 encode the MP3 which can then be fed into the src attribute of an <audio> tag.
Because it is unrealistically to provide a base64 encoded MP3 in an answer here I'll provide a Fiddle: https://jsfiddle.net/4t6bg95z/1/
But the gist of the code can be something like:
var audio = document.getElementById('audio');
audio.src = "data:audio/mp3;base64,..."; //This is a base64 encoded string of an MP3 file
window.beep = function() {
audio.play();
}
Obviously, it is much better practice to provide a URL to the audio source instead, as that's the intended usage of the Audio API.
I am trying to play a base 64 encoded MP4 using the HTML5 data URI. For some reason, the video won't play. The approach works fine for image files (jpeg and png), however for some reason it won't work for media files.
On the client side I have:
document.getElementById("video_holder").src = "data:video/mp4;base64," + xmlhttp.responseText;
HTML something like:
<video controls> <source type="video/webm" src="" id="video_holder" />...
I am fetching the file using PHP and base64 encode it before sending it to the user:
$file = file_get_contents($path);
echo chunk_split(base64_encode(trim($file)));
I also tried:
$file = file_get_contents($path);
echo base64_encode(trim($file));
Any suggestions?
I would say you first need to decode in JavaScript your Base 64 file. This is explained here but some browsers do not support it.
I would then create a Blob object from the decoded file and then create an objectURL from the blob and push this objectURL as the src of the HTML5 video tag.
I have not done this yet but this is how I would do it if I had to. Why do you need to base64 encode your video file in the first place? This can create overhead and increase processing time. If you need encryption a DRM solution would be better.
We would like to have links in a word press site that have the current meta description of the target site as the anchor text of the link.
I understand this requires either javascript or php and am not sure which is the appropriate approach and which is most easily supported within word press.
If you have Wordpress then you should have cURL installed and activated (or find the way). Also, there is a PHP function called get_meta_tags(). So, you could do something like this assuming you have an array of links with each URL called $links_array:
foreach($links_array as $link){
$tags = get_meta_tags($link);
$description = #$tags['description'];
//Printing each link
echo "<a href='$link'>$description</a>";
}
Interesting question and yes it is possible. You can't do it with javascript or AJAX because the browsers' cross-domain policy won't allow you to do this. I think it has to be a combination of both.
The first solution that i can think of is creating some kind of proxy with PHP, that returns the contents of the targeted URL (the one you link to):
<?php
$url=$_POST['url'];
if($url!="")
echo file_get_contents($url);
?>
Lets say we call this little script "getit.php". Now you can get a AJAX call going, that sends the target url to your .php file and the .php file returns the content of the targeted page. Then you are going to extract the description meta-tag from the returned data.
Of course you could get it in the PHP file and only return the meta description, because that would even be a better solution. You could try something like this in the PHP:
<?php
$url=$_POST['url'];
$tags = get_meta_tags($url);
return $tags['description'];
?>
PS. Apologies for my bad English, it's not my native language.
My project requires me to add a "SaveAs PDF" feature. I was looking for a pure JavaScript solution which does this just in client end, however, was told that it's not implementable, at least for now.
jsPDF currently is still a limited version, not support graph and others. So now I am looking for a stable open-srouce free solution to set up a server web service, that receive data from client-end and send back the produced PDF file or a link for user to save to their disk.
The data from client is determined by client user, which means, not the whole page. User can choose to save a map, a table, or all of them into PDF file.
Any recommendations?
PS: in Windows environment
You might check out the ReportLab Toolkit - it includes an Open Source Python library for creating PDFs. (You didn't specify what server-side language you wanted, but Python is pretty widely supported.)
If you need something that can be coded in Javascript, one option might be PhantomJS. This tool allows you to run a headless Webkit browser from the command line, and among other things it can render and save webpages as PDFs. Slippy uses this approach, so you might be able to get example code from that project. Scripting the PDF creation would probably be much faster in PhantomJS than in Python, but it's likely to be much slower (it has to fire up a Webkit instance) and server installation might be complicated.
I've create this function in javascript which send on iframe to the server:
function download(url, datas){
if(url && datas){
var inputs = '', value,
iframe = '<iframe name="iframeDownload" id="iframeDownload" width=0 height=0></iframe>';
$(iframe).appendTo('body');
datas.forEach(function(data){
name = encodeURI(data.get('name'));
value = encodeURI(data.get('value'));
inputs+='<input name="'+name+'" value="'+value+'"/>';
});
$('<form action="'+url+'" method="post" target="iframeDownload">'+inputs+'</form>').appendTo('body').submit().remove(); // .appendTo and remove() are needed for firefox
$(iframe).remove();
};
};
I'm encoding the input name and value to be able to send data.
On my server, I'm using php, so to decode this, you need: rawurldecode. If you define the name of the inputs as "fileName" and "file" you can write this:
$fileName = rawurldecode($_POST['fileName']);
$file = rawurldecode($_POST['file']);
After than, to force the download, you need to send the corrects header. I'm using this function:
function download($filename, $file) {
header('Content-disposition: attachment; filename="'.$filename.'"');
header('Content-Type: application/force-download');
header('Content-Length: '. filesize($file));
readfile($file);
}
If you don't need to send the file from javascript because it's created on the server side, just add the path of your file to the download function.
If you're using PHP, You can use fpdf to generate the pdf.