Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtitles for <audio> with <track>, how to display the subtitles

I'm trying to add a text transcription of a spoken audio file with the track tag. The default behavior for the video tag is to display them (works). By default the audio tag seems to lack some sort of 'canvas' (the black area a video tag displays even without video) to display the subtitles automatically. I could use the video tag but it would feel like a ugly workaround. I don't want to break the semantics of my code though.

Is there some kind of CSS to force the display of such area where the subtiles will be displayed?

<audio controls>   <source src="test.ogg" type="audio/ogg">   <source src="test.mp3" type="audio/mpeg">   <track kind="subtitles" label="English subtitles" src="/test.vtt" srclang="en" default></track>   Your browser does not support the audio tag. </audio> 

Thanks you for reading.

like image 598
phl Avatar asked May 05 '14 14:05

phl


People also ask

How do I display Subtitles?

Open your device's Settings. Select Accessibility, then select Captions (labeled “Caption Preferences” on some devices) Toggle the On/Off switch to your preference.

How do I get captions from audio?

In Audio Tools, select the Playback tab, and select the Add Captions menu. Select Captions From File. Browse to the TTML file location, select the file you want to import, and select Open. Select OK.

Can Subtitles be read by screen readers?

Even with closed captions, some captions will not be seen by screen readers. For most screen reader users who are blind, but are not deaf or hard of hearing, they may not use the captions because they can hear the audio.


2 Answers

I've tested this out without jquery -- as taylor-newton mentioned, you'll need to create a tag for your text to appear in.

document.getElementById('my-audio-player').textTracks[0].addEventListener('cuechange', function() {     document.getElementById('my-subtitle-display').innerText = this.activeCues[0].text; }); 

This does work with subtitles from audio tags too and using kind="subtitles" in your track tag works with audio as well.

like image 69
Stuart Mclean Avatar answered Sep 23 '22 23:09

Stuart Mclean


I don't know how to do this with just CSS, but I have done something similar (custom cues) with video text tracks and JavaScript. Hopefully you should be able to leverage the same TextTrack events to accomplish what you are wanting to do with audio tracks.

You can bind a custom function to the track's oncuechange event, and then use the track's activeCues to generate your own captions. This custom div can then be positioned or styled however you want.

This should grab the text track and get the text from the currently active cue every time a cue change occurs.

$('audio')[0].textTracks[0].oncuechange = function() {      var currentCue = this.activeCues[0].text;     $('#yourCustomCaptions').html(currentCue); } 

Then take the text from each cue and inject it into the custom div you want to display.

https://developer.mozilla.org/en-US/docs/Web/API/TextTrack

like image 37
Taylor Newton Avatar answered Sep 25 '22 23:09

Taylor Newton