Phonegap TTS Plugin Android not working

…衆ロ難τιáo~ 提交于 2019-12-03 00:33:58

After some struggle i have the TTS working. But there is still one issue i had to manually fix. Following are the steps to get the TTS Working

Install the plugin like below.

phonegap plugin add https://github.com/domaemon/org.apache.cordova.plugin.tts.git
phonegap build android

Once installed and built. Add this plugin to the phonegap config.xml file. ( If you are building the app using sencha touch, the config.xml will be in the root folder. )

<gap:plugin name="org.apache.cordova.plugins.tts" value="org.apache.cordova.plugins.tts"/>

This will add the plugin to the final build. Now to start the TTS Service and speak some text, use the following snippet.

navigator.tts.startup(startupWin, fail);
function startupWin(result) {
    console.log("Startup win");
    // When result is equal to STARTED we are ready to play
    console.log("Result "+result);
    //TTS.STARTED==2 use this once so is answered
    if (result == 2) {
        navigator.tts.getLanguage(win, fail);
        navigator.tts.speak("The text to speech service is ready");                                     
    }
}                               

function win(result) {
    console.log(result);
}

function fail(result) {
    console.log("Error = " + result);
}

The issue i had was the TTS.STARTED in the startupWin is not defined in the plugin. I just used the constant's value and the plugin works perfectly.

result == 2 or STARTED works only once. If you again call the function it may not return 2 or STARTED (happened with me). so better dont use that condition in success of startup.

/*********tts.js*************/
var tts = {
    say: function() {
	alert("tts");
    },
    startup: function(successCallback, errorCallback) {
	console.log("TTS-Startup");
        cordova.exec(successCallback, errorCallback, "TTS", "startup", []);
    },
    speed: function(speed, successCallback, errorCallback) {
    cordova.exec(successCallback, errorCallback, "TTS", "speed", [speed]);
    },
    speak: function(text, successCallback, errorCallback) {
    	cordova.exec(successCallback, errorCallback, "TTS", "speak", [text]);
    }
  };

if(!window.plugins) {
    window.plugins = {};
}

if (!window.plugins.tts) {
    window.plugins.tts = tts;
}
/**********calling from your js  after device ready***************/
 function visitToString(){
  window.plugins.tts.startup(function(result){
			window.plugins.tts.speed(50,function(){
				console.log('speed success');
			},function(err){
				console.log('speed err'+JSON.stringify(err));
			});  
	    	 window.plugins.tts.speak(finalstr,function(){
	    		 console.log('speech success');
	    	 },function(err){
	    		 console.log('speech err'+JSON.stringify(err));
	    	 });  
		    
	}, fail);
  }
<button id="speakvisit" onclick="visitToString();">Audio Details
					</button>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!