Pravin Kumar M
Pravin Kumar M

Reputation: 13

How to play blob mysql audio data in Html5 Audiio tag...?

I've finished to store audio blob(binary) data in to MySQL DB. But how can i show that blob data in my HTML page for a audio tag.

Upvotes: 0

Views: 1639

Answers (1)

MKP
MKP

Reputation: 200

just create a PHP script that will fetch the audio data from DB and write it as a result and (the most important) change the result page content-type. Here is an example of such basic script:

<?php
$mysqli = new mysqli("localhost", "user", "pass", "testDB");

$query = "SELECT * FROM testTable";

if ($result = $mysqli->query($query)) {
    $row = $result->fetch_assoc();

    header("Content-type: audio/mpeg"); 
    echo $row['audioData'];

    $result->free();
}

$mysqli->close();
?>

The most important line is:

header("Content-type: audio/mpeg");

which tells the browser to interpret the content as binary audio data. This is an example for mp3 files. If you use other encoding you have to change audio/mpeg to proper value of your file (you can find proper values easily in the Internet).

And simple HTML page to use this script (look carefully on the src value of audio source tag:

<!DOCTYPE html>
<html>
<body>

<audio controls>
  <source src="yourScript.php" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>

</body>
</html>

I hope it helps :)

Upvotes: 1

Related Questions