Search code examples
htmlaudiomp4

How can I use html audio tag with icon?


I'm using an html audio control in a webpage to play an mp4 file.

<audio controls>
  <source src="example.m4a" type="audio/mp4">
</audio>

The tag displays a visual control specific to the platform.

enter image description here

How can I use the tag so that it displays a clickable icon of my choice instead of the default visual control?

enter image description here


Solution

  • Maybe like this :

        let a=0;document.getElementsByTagName("i")[0].addEventListener("click",function(){
    if(a==0){
    document.getElementsByTagName("audio")[0].play();a++;document.querySelector("i").style.opacity="1";
    }else{
    document.querySelector("audio").pause();a--;document.querySelector("i").style.opacity=".5";
    }
    });
    <head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    </head>
    <body>
    
    <div>
    <h1 style="display:inline;color:grey;font-family:sans-serif;font-weight:normal;">Prison Walls</h1>
    <span><i class="fa fa-volume-up fa-2x" style="color:grey;opacity:.5"></i></span>
    </div>
    <audio hidden>
      <source src="https://files.freemusicarchive.org/storage-freemusicarchive-org/music/ccCommunity/Julie_Maxwells_Piano_Music/Classic_Piano_Collection_from_the_Princess_of_Mars/Julie_Maxwells_Piano_Music_-_21_-_Prison_Walls.mp3" type="audio/mp3">
    </audio>
    </body>

    I used javascript addEventListener with traitement. So that, you can play or pause the audio. Also you can found the volume up icon you are looking for on https://fontawesome.com/icons/volume-up?style=solid.