How to customize the audio html element controls?

Asked

Viewed 2,736 times

0

What I intend to do is customize the controls of the audio html element. I need to present in the Front End only one icon (the speaker), and this should have only the function of switching on and off audio mp3.

An example of what I need is google Translator. The button to hear the pronunciation of the translation made.

I accept another suggestion that gives the desired result.

At the moment the audio presents this layout:

inserir a descrição da imagem aqui

How I need:

inserir a descrição da imagem aqui

The code I’m using:

 <audio controls src="audio.mp3" >
<p>Seu navegador não suporta o elemento audio </p>
</audio>

1 answer

2


This is a model that can help you.

/* 
Esse modelo também funciona para vídeo basta trocar a tag <audio> por <video>:
 
  <video width="1280" height="720" >
    <source src="movie.mp4" type="video/mp4">
    <source src="movie.ogg" type="video/ogg">
  </video> 
  
*/

// Pega o ID do audio
var podcastAudio = document.getElementById('podcast-audio');

var playBtn = document.getElementById('podcast-play');

var pauseBtn = document.getElementById('podcast-pause');

// Play audio & mostra pause btn
var playShow = function() {
  podcastAudio.play();
  playBtn.style.display = "none";
  pauseBtn.style.display = "inline-block";
};

// Pause audio & mostra play btn
var pauseShow = function() {
  podcastAudio.pause();
  playBtn.style.display = "inline-block";
  pauseBtn.style.display = "none";
};
.player-ctrl {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #569ef7;
  height: 100vh;
  width: 100%;
}
i {
  font-size: 50px;
  color: white;
  line-height: 50px;
  padding: 13px;
}
h1 {
  font-family: sans-serif;
  padding-left: 20px;
  color: #222;
  font-weight: 300;
}
a {
  background-color: #fb5f70;
  border-radius: 5px;
  padding: 20px;
  width: 50px;
  height: 50px;
  cursor: pointer;
}
<link rel='stylesheet' href='https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css'>
<div class="player-ctrl">

  <!-- Play Button -->
  <a id="podcast-play" onclick="playShow()"><i class="ion-ios-play"></i></a>

  <!-- Pause Button -->
  <a id="podcast-pause" onclick="pauseShow()" style="display:none;"><i class="ion-ios-pause"></i></a>
  <h1>Click me</h1>
</div>


<!-- Audio player -->
<audio id="podcast-audio">
    <source src="https://s3.amazonaws.com/cmb-portfolio-16/closer.m4a" type="audio/mp4">
</audio>

Source: https://codepen.io/coreybrown89/pen/QKyKYy

You can use some library ready type http://www.jplayer.org/

Or you can make in hand, this article gives a tips: https://webdesign.tutsplus.com/pt/tutorials/create-a-customized-html5-audio-player--webdesign-7081 and here too: https://www.theparticlelab.com/building-a-custom-html5-audio-player-with-jquery/

Another similar question in Stackoverflow: https://stackoverflow.com/questions/4126708/is-it-possible-to-style-html5-audio-tag/33948255#33948255

Browser other questions tagged

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