ak1394 / react-native-tts

React Native Text-To-Speech library for Android and iOS
618 stars 156 forks source link

Character limit for TTS.speak? #114

Open brystfire08 opened 5 years ago

brystfire08 commented 5 years ago

I have a long string to read around 4000+ characters long. I noticed that when I put around 3900+ characters for TTS.speak first parameter it throws an error:

Possible Unhandled Promise Rejection (id: 0): Error: Language data is missing

Edit: exact allowed characters is 3999 more than that it throws the error

brystfire08 commented 5 years ago

So for now i did it like this.

let veryLongString = "very long string ....";
if (veryLongString.length >= 3999) {
    function splitNChars(txt, num) {
        var result = [];
        for (var i = 0; i < txt.length; i += num) {
            result.push(txt.substr(i, num));
        }
        return result;
    }

    var splitMe = splitNChars(veryLongString,3999);

    splitMe.forEach(function (value, key) {                                                             
        Tts.speak(value, {
            androidParams: {                                        
                KEY_PARAM_STREAM: "STREAM_MUSIC"
            }
        });
    });
}
else {
    Tts.stop();
    Tts.speak(escaped, {
        androidParams: {                                    
            KEY_PARAM_STREAM: "STREAM_MUSIC"
        }
    });
}
ra1111 commented 3 years ago

So for now i did it like this.

let veryLongString = "very long string ....";
if (veryLongString.length >= 3999) {
  function splitNChars(txt, num) {
      var result = [];
      for (var i = 0; i < txt.length; i += num) {
          result.push(txt.substr(i, num));
      }
      return result;
  }

  var splitMe = splitNChars(veryLongString,3999);

  splitMe.forEach(function (value, key) {                                                             
      Tts.speak(value, {
          androidParams: {                                        
              KEY_PARAM_STREAM: "STREAM_MUSIC"
          }
      });
  });
}
else {
  Tts.stop();
  Tts.speak(escaped, {
      androidParams: {                                    
          KEY_PARAM_STREAM: "STREAM_MUSIC"
      }
  });
}

Thanks a lot! The problem has not been well documented but you found the issue and resolved it also.