How to download audio generated with ResponsiveVoice in an MP3 file with Javascript?

4

I am using responsivevoice.js to generate audio from text and I want to save the audio in mp3 .

<script src="http://code.responsivevoice.org/responsivevoice.js"></script>
<script type="text/javascript">
  responsiveVoice.setDefaultVoice("Spanish Female");
  responsiveVoice.speak("Hola");
</script>
    
asked by lanoch 22.12.2016 в 13:01
source

1 answer

7

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>
    
answered by 22.12.2016 / 15:12
source