How to play a .mp3 file in PHP

2

I'm learning PHP and I need your help. I saw in a post here that it is possible to touch a .mp3 file in PHP.

I did a test like this:

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

The 11.mp3 file is in the same FTP folder as teste.php . But he's not touching anything. Could someone tell me why?

    
asked by anonymous 01.09.2016 / 04:44

1 answer

7

In fact you are not "playing a .mp3 in php", I believe that in order to achieve EXACTLY that the player itself should be built in PHP, I do not even know if that exists.

But I understood what you meant.

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

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

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

Want a demo?!

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

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

  

The preview of the song, used in the example, was taken from the Deezer API , exactly here .

You DO NOT 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 do not shy away from it.

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

    
01.09.2016 / 05:26