Help

Difference between revisions of "Embed audio in HTML"

Line 29: Line 29:
 
* https://www.w3schools.com/tags/tag_audio.asp
 
* https://www.w3schools.com/tags/tag_audio.asp
  
=== Using HTML5, Jquery.js with fade-in and fade-out ===
+
=== HTML5 & light javascript ===
See [https://codepen.io/hugolpz/pen/QWGyVwM '''Audio 101''' – with typical cases parameters and behaviors]
+
* [https://codepen.io/hugolpz/pen/QWGyVwM '''Audio 101''' – with typical cases duration, volume parameters and fade-in, fade-out behaviors]
 
+
* [https://codepen.io/hugolpz/pen/NWbNWWK '''Audio 101''' – with play, pause, toogle UI]
'''Using HTML5, Jquery.js and Bootstrap.css'''
 
 
<pre>
 
<pre>
 
<div class="audio">
 
<div class="audio">
Line 47: Line 46:
 
   </button>
 
   </button>
 
</div>
 
</div>
<script>var waitTime = 150;
+
<script>
 +
var waitTime = 150;
 
var playItemToggle = function(item) {
 
var playItemToggle = function(item) {
 
   setTimeout(function () {  
 
   setTimeout(function () {  
Line 63: Line 63:
 
</pre>
 
</pre>
  
;Example
+
'''References'''
* https://codepen.io/hugolpz/pen/NWbNWWK
 
 
* https://www.w3schools.com/tags/ref_av_dom.asp
 
* https://www.w3schools.com/tags/ref_av_dom.asp
  

Revision as of 01:15, 6 February 2021

Given an audio file Bazinga.mp3.

Using Howler JS

HTML
<script src="./howler-2.0.8.js"></script>
<buton id="bazingaId" class="audio">Bazinga</button>
Javascript
$('.audio').on('click', function() {  
  var sound = new Howl({ src: [ '/to/folder/Bazinga.mp3','/to/folder/Bazinga.ogg' ]});
  sound.play();
});
References

HTML5 audio only

<audio controls="controls">
  <source src="/to/folder/Bazinga.ogg" type="audio/ogg">
  <source src="/to/folder/Bazinga.mp3" type="audio/mpeg"><!-- fallback -->
  Your browser does not support HTML5 audio. Please download and install a modern browser.
</audio>
References

HTML5 & light javascript

<div class="audio">
  <audio id="" preload="auto">
    <source src="https://upload.wikimedia.org/wikipedia/commons/3/3d/Africanagogosound.ogg" type="audio/ogg">
    Your browser does not support HTML5 audio.
  </audio>

  <button class="btn btn-primary">
    <span class="play"><text>⏵</text></span>
  </button>
  <button class="btn btn-info" style="display:none;">
    <span class="pause"><text>⏸</text></span>
  </button>
</div>
<script>
var waitTime = 150;
var playItemToggle = function(item) {
  setTimeout(function () { 
    if (item.paused) { /*item.load();*/ item.play() } 
    else { item.pause(); }
  }, waitTime);
} 

$('.audio').on('click', function() {
  var $audio = $(this).find('audio')[0],
  $btns  = $(this).find('.btn');
  $btns.toggle();
  playItemToggle($audio);
});</script>

References

Audio controls characters

  • &#x23e9;
  • &#x23ea;
  • &#x24eb;
  • &#x23ec;
  • &#x23ed;
  • &#x23ee;
  • &#x23ef;
  • &#x23f4;
  • &#x23f5;
  • &#x23f6;
  • &#x23f7;
  • &#x23f8;
  • &#x23f9;
  • &#x23fa;

See HTML symbol in audio and video controls