Help

Difference between revisions of "Embed audio in HTML"

This page present various practical ways to include LinguaLibre's audio within your web applications or projects. Cases showcasted below goes from easy to more complex. You are encouraged to hack and play with them on jsfiddle, codepen or other coding pads. A larger json with filenames and some for-loop will then allow you to take full advantage of Lingualibre's large datasets.

 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{#SUBTITLE:This page present various practical ways to include LinguaLibre's audio within your web applications or projects. Cases showcasted below goes from easy to more complex. You are encouraged to hack and play with them on jsfiddle, codepen or other coding pads. A larger json with filenames and some for-loop will then allow you to take full advantage of Lingualibre's large datasets.}}
 +
=== HTML5 audio only ===
 
Given an audio file <code>Bazinga.mp3</code>.
 
Given an audio file <code>Bazinga.mp3</code>.
 +
<pre>
 +
<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>
 +
</pre>
 +
 +
;References
 +
* https://www.w3schools.com/tags/tag_audio.asp
 +
 +
=== HTML5 & light javascript ===
 +
* [https://codepen.io/hugolpz/pen/QWGyVwM?editors=1100 '''Audio 101''' – with duration, volume parameters and fade-in, fade-out behaviors]
 +
* [https://codepen.io/hugolpz/pen/NWbNWWK '''Audio 101''' – with play, pause, toogle UI]
 +
<pre>
 +
<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>
 +
</pre>
 +
 +
'''References'''
 +
* https://www.w3schools.com/tags/ref_av_dom.asp
  
=== Using Howler JS ===
+
=== Howler.js : advanced audio library ===
 
;HTML
 
;HTML
  
<pre><script src="./howler-2.0.8.js"></script>
+
<pre>
<buton id="bazingaId" class="audio">Bazinga</button></pre>
+
<script src="./howler-2.0.8.js"></script>
 +
<buton id="bazingaId" class="audio">Bazinga</button>
 +
</pre>
  
 
;Javascript
 
;Javascript
  
<pre>$('.audio').on('click', function() {   
+
<pre>
 +
$('.audio').on('click', function() {   
 
   var sound = new Howl({ src: [ '/to/folder/Bazinga.mp3','/to/folder/Bazinga.ogg' ]});
 
   var sound = new Howl({ src: [ '/to/folder/Bazinga.mp3','/to/folder/Bazinga.ogg' ]});
 
   sound.play();
 
   sound.play();
});</pre>
+
});
 +
</pre>
  
 
;References
 
;References
 
* https://howlerjs.com
 
* https://howlerjs.com
  
=== Using HTML5 audio ===
+
=== Audio controls characters ===
<pre>
+
* ⏩ <code>&amp;#x23e9;</code>
<audio controls="controls">
+
* ⏪ <code>&amp;#x23ea;</code>
  <source src="/to/folder/Bazinga.ogg" type="audio/ogg">
+
* ⏫ <code>&amp;#x24eb;</code>
  <source src="/to/folder/Bazinga.mp3" type="audio/mpeg"><!-- fallback -->
+
* ⏬ <code>&amp;#x23ec;</code>
  Your browser does not support HTML5 audio. Please download and install a modern browser.
+
* ⏭ <code>&amp;#x23ed;</code>
</audio>
+
* ⏮ <code>&amp;#x23ee;</code>
</pre>
+
* ⏯ <code>&amp;#x23ef;</code>
 +
* ⏴ <code>&amp;#x23f4;</code>
 +
* ⏵ <code>&amp;#x23f5;</code>
 +
* ⏶ <code>&amp;#x23f6;</code>
 +
* ⏷ <code>&amp;#x23f7;</code>
 +
* ⏸ <code>&amp;#x23f8;</code>
 +
* ⏹ <code>&amp;#x23f9;</code>
 +
* ⏺ <code>&amp;#x23fa;</code>
 +
See [https://stackoverflow.com/questions/22885702/html-for-the-pause-symbol-in-a-video-control HTML symbol in audio and video controls]
 +
 
 +
== See also ==
 +
{{Technicals}}
  
;References
+
[[Category:Lingua Libre:Help]]
* https://www.w3schools.com/tags/tag_audio.asp
 

Latest revision as of 20:44, 28 December 2023

HTML5 audio only

Given an audio file Bazinga.mp3.

<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

Howler.js : advanced audio library

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

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

See also

Lingua Libre technical helps
Template {{Speakers category}} • {{Recommended lists}} • {{To iso 639-2}} • {{To iso 639-3}} • {{Userbox-records}} • {{Bot steps}}
Audio files How to create a frequency list?Convert files formatsDenoise files with SoXRename and mass rename
Bots Help:BotsLinguaLibre:BotHelp:Log in to Lingua Libre with PywikibotLingua Libre Bot (gh) • OlafbotPamputtBotDragons Bot (gh)
MediaWiki MediaWiki: Help:Documentation opérationelle MediawikiHelp:Database structureHelp:CSSHelp:RenameHelp:OAuthLinguaLibre:User rights (rate limit) • Module:Lingua Libre record & {{Lingua Libre record}}JS scripts: MediaWiki:Common.jsLastAudios.jsSoundLibrary.jsItemsSugar.jsLexemeQueriesGenerator.js (pad) • Sparql2data.js (pad) • LanguagesGallery.js (pad) • Gadgets: Gadget-LinguaImporter.jsGadget-Demo.jsGadget-RecentNonAudio.js
Queries Help:APIsHelp:SPARQLSPARQL (intermediate) (stub) • SPARQL for lexemes (stub) • SPARQL for maintenanceLingualibre:Wikidata (stub) • Help:SPARQL (HAL)
Reuses Help:Download datasetsHelp:Embed audio in HTML
Unstable & tests Help:SPARQL/test
Categories Category:Technical reports