unknown
unknown

Reputation: 1

How to check whether audio file exists or not in html5

<audio controls>

  <source src="test.mp3" type="audio/mpeg">

</audio>

I want to display audio tag only if audio file exists . Please help me to resolve this issue

Upvotes: 0

Views: 2226

Answers (2)

Parithiban
Parithiban

Reputation: 1656

You need to check file exists

  <?php 
    if (file_exists('test.mp3')) {
    ?>
        <audio controls>
         <source src="test.mp3" type="audio/mpeg">
        </audio>
    <?php } ?>

Upvotes: 0

Jay Ghosh
Jay Ghosh

Reputation: 682

Make an ajax request to the audio file you need.The response will let you know if the file exists or not

$.ajax({
url:'test.mp3',
type:'HEAD',
error: function()
{
    //file does not exist
},
success: function()
{
    //file exists
}
});

Upvotes: 1

Related Questions