How to play a. mp3 file in PHP

Asked

Viewed 3,311 times

2

I am learning PHP and I need your help. I saw it in a post here it is possible to play a file .mp3 in PHP.

I took a test like this:

<?php
echo "Teste";
echo "<embed src='11.mp3' width='1' height='1'>";
?>

The archive 11.mp3 is in the same FTP folder as teste.php.
But it’s not touching anything. Would someone tell me why?

1 answer

7

In fact you are not "playing a . mp3 in php", I believe that to achieve this the player itself should be built in PHP, I don’t even know if there’s such a thing.

But I understand what you mean.

Your code just "displays" a text for the user, using the echo. This "text" is an HTML.

One solution would be to use the audio HTML instead of embed as:

<audio id="audio" autoplay>
   <source src="11.mp3" type="audio/mp3" />
</audio>

You want a demonstration?!

 <audio id="audio" autoplay controls>
    <source src="https://cdns-preview-8.dzcdn.net/stream/821246fb5d7e2ff6975f65ef7460a708-0.mp3" type="audio/mp3">
</audio>

The function audio has several different attributes, including in this example the controls (to display the controls, pause and such) and the autoplay (to start automatically). You can all attributes here. ;)

The music preview, used in the example, was extracted from the API of Deezer, right here.

You DON’T NEED PHP for this.

If you really want to use PHP to display, you can use:

echo '<audio id="audio" autoplay>
   <source src="11.mp3" type="audio/mp3" />
</audio>';

There are other alternatives, but it doesn’t go away.

Also check the Console (F12 in Google Chrome) and enter "Network" and see if he is getting the file 11.mp3. If it is 404, or any value other than 200, it may not be able to find the file 11.mp3.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.