How to add HTML 5 audio player?

I am getting the original file from a folder. When I select a finished mp3 file like a song as src in the audio tag, it works. But when I select the mp3 file (call recording file) generated from the base64 encoded string, I don't work. The file created fine when I checked the folder and played. The problem is that when I move the cursor over the player, it becomes transparent like an image. If anyone knows the answer, please explain with an example. Thank. Here is my code.

<?php

$data = $_REQUEST['data'];
$filename = $_REQUEST['filename'] . ".3gpp";
$imei = $_REQUEST['imei'];

$dir = __DIR__ . "/recordings/";
$file = file_put_contents($dir . $filename, base64_decode($data));
$rloc = "recordings/" . $filename;

$str = "INSERT INTO recording(data, filename, imei) VALUES('$rloc', '$filename', '$imei')";
$qry = mysql_query($str);

?>
<html>
<audio controls autoplay="">
       <source src="<?php echo $row['data']; ?>">
</audio>
</html>

      

+3


source to share


5 answers


<audio controls="controls" autobuffer="autobuffer" autoplay="autoplay">
<source src="data:audio/wav;base64,UklGRhwMAABXQVZFZm10IBAAAAABAAEAgD4AAIA+AAABAAgAZGF0Ya4LAACAgICAgICAgICAgICAgICAgICAgICAgICAf3hxeH+AfXZ1eHx6dnR5fYGFgoOKi42aloubq6GOjI2Op7ythXJ0eYF5aV1AOFFib32HmZSHhpCalIiYi4SRkZaLfnhxaWptb21qaWBea2BRYmZTVmFgWFNXVVVhaGdbYGhZbXh1gXZ1goeIlot1k6yxtKaOkaWhq7KonKCZoaCjoKWuqqmurK6ztrO7tbTAvru/vb68vbW6vLGqsLOfm5yal5KKhoyBeHt2dXBnbmljVlJWUEBBPDw9Mi4zKRwhIBYaGRQcHBURGB0XFxwhGxocJSstMjg6PTc6PUxVV1lWV2JqaXN0coCHhIyPjpOenqWppK6xu72yxMu9us7Pw83Wy9nY29ve6OPr6uvs6ezu6ejk6erm3uPj3dbT1sjBzdDFuMHAt7m1r7W6qaCupJOTkpWPgHqAd3JrbGlnY1peX1hTUk9PTFRKR0RFQkRBRUVEQkdBPjs9Pzo6NT04Njs+PTxAPzo/Ojk6PEA5PUJAQD04PkRCREZLUk1KT1BRUVdXU1VRV1tZV1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KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KCgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGBgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCAgICBgIGAgYCBgIGAgYCBgIGAgYCBgExJU1RCAAAASU5GT0lDUkQMAAAAMjAwOC0wOS0yMQAASUVORwMAAAAgAAABSVNGVBYAAABTb255IFNvdW5kIEZvcmdlIDguMAAA" />
</audio>

      



A data URI takes the format: data: [] [; encoding =] [; base64], The MIME type indicates what data types the URI contains. The encoding in which it is encoded. last encoded data

0


source


You need a prefix data:audio/mp3;base64,

in the data URI. Use this instead:



<audio controls autoplay="">
    <source src="data:audio/mp3;base64,<?php echo $row['data']; ?>">
</audio>

      

0


source


It looks like you are using the wrong variable to set the source. You don't have $row['data']

one defined in your php code. Maybe it should be $rloc

instead:

<audio controls autoplay="">
    <source src="<?php echo $rloc; ?>">
</audio>

      

Edit:

Have you converted the downloaded file (3gpp) to other formats (mp3, ogg) or just renamed it to a different extension? If you look at the binary data in the links you posted in the comments to the other answer, this is your situation: all the links you posted are exactly the same file with a different name. Binary, they are identical, just the filename is different. Format information for the file that is on all your links:

Format                                   : MPEG-4
Format profile                           : 3GPP Media Release 4
Codec ID                                 : 3gp4

      

You can't just change the file extension and expect it to play in the browser. You need to convert the file to a different format. You can use ffmpeg or any other conversion tool to convert from 3gpp to mp3 and ogg.

0


source


I found this short example:

 <audio controls autoplay>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio> 

      

Help.

0


source


You can use data URIs for audio as others have suggested. However, this is not a good idea. Not only are you adding 33% overhead for this encoding and CPU overhead on both ends, but there is also a 1MB limit which won't take long to play audio.

If you must process your audio data this way, you can simply link to your PHP script, which outputs nothing but raw binary audio data.

<audio src="/audio.php?id=12345"></audio>

      

Be sure to set the appropriate title Content-Type

.

Finally, please note that you have serious security issues in your code. Nowadays, anyone can write almost anything they want to any path on your hard drive, because you let them specify the name of a file on the drive that it can contain ../

. Also, your SQL is exposed to SQL injection attacks. Use prepared / parameterized queries to avoid this problem entirely.

0


source







All Articles