There is a API
( responsivevoice.org/responsivevoice/getvoice.php
) that offers ResponsiveVoice
to generate audios dynamically.
Download using JS and HTML5
Using this API
and the attribute download
, you could do the following:
Attribute download
: Not supported by IE
, only from Edge
. In addition Firefox
only supports download links from the same source, so, instead of downloading it, you will be redirected to the site.
var texto = document.getElementById('texto');
var play = document.getElementById('play');
var download = document.getElementById('download');
play.onclick = function() {
responsiveVoice.speak(texto.value, 'Spanish Female');
}
texto.onblur = function () {
var url = '//responsivevoice.org/responsivevoice/getvoice.php?t=' +
encodeURIComponent(texto.value) + '&tl=es-ES';
download.href = url;
}
texto.onblur();
<script src="http://code.responsivevoice.org/responsivevoice.js"></script>
Texto: <input id="texto" value="hola mundo" /><br/>
<button type="button" id="play">Play</button>
<a download id="download">Descargar</a>
Download using API own
Another solution ( and for that matter supports all browsers ) would be that you implement your own API
to download the audio.
Using PHP
you could do the following:
File getaudio.php
<?php
$texto = $_GET['texto'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=audio.mp3;");
// Usamos la API de ResponsiveVoice para generar el audio
echo file_get_contents('http://responsivevoice.org/responsivevoice/getvoice.php?t=' . urlencode($texto) . '&tl=es-ES');
exit;
?>
HTML file
<script src="http://code.responsivevoice.org/responsivevoice.js"></script>
Texto: <input id="texto" value="hola mundo" /><br/>
<button type="button" id="play">Play</button>
<a target="_blank" id="download">Descargar</a>
<script>
var texto = document.getElementById('texto');
var play = document.getElementById('play');
var download = document.getElementById('download');
play.onclick = function() {
responsiveVoice.speak(texto.value, 'Spanish Female');
}
texto.onblur = function () {
// Usamos NUESTRA API para descargar el audio
var url = '//dominio.com/path/getaudio.php?texto=' +
encodeURIComponent(texto.value);
download.href = url;
}
texto.onblur();
</script>