Here the code I have done to add a callback function. You can modify it as you want, I don't think that the name of variable are very good.
for tts.js
/*
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2011, IBM Corporation
*/
/**
* Constructor
*/
function TTS() {
}
TTS.STOPPED = 0;
TTS.INITIALIZING = 1;
TTS.STARTED = 2;
/**
* Play the passed in text as synthasized speech
*
* @param {DOMString} text
* @param {Object} successCallback
* @param {Object} errorCallback
*/
TTS.prototype.speak = function(text, successCallback, errorCallback) {
return PhoneGap.exec(successCallback, errorCallback, "TTS", "speak", [text]);
};
/**
* Play silence for the number of ms passed in as duration
*
* @param {long} duration
* @param {Object} successCallback
* @param {Object} errorCallback
*/
TTS.prototype.silence = function(duration, successCallback, errorCallback) {
return PhoneGap.exec(successCallback, errorCallback, "TTS", "silence", [duration]);
};
/**
* Starts up the TTS Service
*
* @param {Object} successCallback
* @param {Object} errorCallback
*/
TTS.prototype.startup = function(successCallback, errorCallback) {
return PhoneGap.exec(successCallback, errorCallback, "TTS", "startup", []);
};
/**
* Shuts down the TTS Service if you no longer need it.
*
* @param {Object} successCallback
* @param {Object} errorCallback
*/
TTS.prototype.shutdown = function(successCallback, errorCallback) {
return PhoneGap.exec(successCallback, errorCallback, "TTS", "shutdown", []);
};
/**
* Finds out if the language is currently supported by the TTS service.
*
* @param {DOMSting} lang
* @param {Object} successCallback
* @param {Object} errorCallback
*/
TTS.prototype.isLanguageAvailable = function(lang, successCallback, errorCallback) {
return PhoneGap.exec(successCallback, errorCallback, "TTS", "isLanguageAvailable", [lang]);
};
/**
* Finds out the current language of the TTS service.
*
* @param {Object} successCallback
* @param {Object} errorCallback
*/
TTS.prototype.getLanguage = function(successCallback, errorCallback) {
return PhoneGap.exec(successCallback, errorCallback, "TTS", "getLanguage", []);
};
/**
* Sets the language of the TTS service.
*
* @param {DOMString} lang
* @param {Object} successCallback
* @param {Object} errorCallback
*/
TTS.prototype.setLanguage = function(lang, successCallback, errorCallback) {
return PhoneGap.exec(successCallback, errorCallback, "TTS", "setLanguage", [lang]);
};
/**
* Sets callback function of the TTS service.
*
* @param {DOMString} js
*/
TTS.prototype.setCallbackFct = function(js, successCallback, errorCallback) {
return PhoneGap.exec(successCallback, errorCallback, "TTS", "setCallbackFct", [js]);
};
/**
* Load TTS
*/
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("tts", new TTS());
// @deprecated: No longer needed in PhoneGap 1.0. Uncomment the addService code for earlier
// PhoneGap releases.
// PluginManager.addService("TTS", "com.phonegap.plugins.speech.TTS");
});
And for TTS.java
/*
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2011, IBM Corporation
*/
package com.phonegap.plugins.speech;
import java.util.HashMap;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
public class TTS extends Plugin implements OnInitListener, OnUtteranceCompletedListener {
private static final String LOG_TAG = "TTS";
private static final int STOPPED = 0;
private static final int INITIALIZING = 1;
private static final int STARTED = 2;
private TextToSpeech mTts = null;
private int state = STOPPED;
private String callbackFct = null;
private String startupCallbackId = "";
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("speak")) {
String text = args.getString(0);
if (isReady()) {
HashMap<String, String> map = null;
if (this.callbackFct != null){
map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "Speak Completed");
}
mTts.speak(text, TextToSpeech.QUEUE_ADD, map);
return new PluginResult(status, result);
} else {
JSONObject error = new JSONObject();
error.put("message","TTS service is still initialzing.");
error.put("code", TTS.INITIALIZING);
return new PluginResult(PluginResult.Status.ERROR, error);
}
}
else if (action.equals("silence")) {
if (isReady()) {
mTts.playSilence(args.getLong(0), TextToSpeech.QUEUE_ADD, null);
return new PluginResult(status, result);
} else {
JSONObject error = new JSONObject();
error.put("message","TTS service is still initialzing.");
error.put("code", TTS.INITIALIZING);
return new PluginResult(PluginResult.Status.ERROR, error);
}
}
else if (action.equals("startup")) {
if (mTts == null) {
this.startupCallbackId = callbackId;
state = TTS.INITIALIZING;
mTts = new TextToSpeech(this.ctx, this); //getContext previously
//mTts.setLanguage(Locale.US);
}
PluginResult pluginResult = new PluginResult(status, TTS.INITIALIZING);
pluginResult.setKeepCallback(true);
return pluginResult;
}
else if (action.equals("shutdown")) {
if (mTts != null) {
mTts.shutdown();
}
return new PluginResult(status, result);
}
else if (action.equals("getLanguage")) {
if (mTts != null) {
result = mTts.getLanguage().toString();
return new PluginResult(status, result);
}
}
else if (action.equals("isLanguageAvailable")) {
if (mTts != null) {
Locale loc = new Locale(args.getString(0));
int available = mTts.isLanguageAvailable(loc);
result = (available < 0) ? "false" : "true";
return new PluginResult(status, result);
}
}
else if (action.equals("setLanguage")) {
if (mTts != null) {
Locale loc = new Locale(args.getString(0));
int available = mTts.setLanguage(loc);
result = (available < 0) ? "false" : "true";
return new PluginResult(status, result);
}
}
else if (action.equals("setCallbackFct")) {
String js = args.getString(0);
this.callbackFct = js;
}
return new PluginResult(status, result);
} catch (JSONException e) {
e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
/**
* Is the TTS service ready to play yet?
*
* @return
*/
private boolean isReady() {
return (state == TTS.STARTED) ? true : false;
}
/**
* Called when the TTS service is initialized.
*
* @param status
*/
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
mTts.setOnUtteranceCompletedListener(this);
state = TTS.STARTED;
PluginResult result = new PluginResult(PluginResult.Status.OK, TTS.STARTED);
result.setKeepCallback(false);
this.success(result, this.startupCallbackId);
}
else if (status == TextToSpeech.ERROR) {
state = TTS.STOPPED;
PluginResult result = new PluginResult(PluginResult.Status.ERROR, TTS.STOPPED);
result.setKeepCallback(false);
this.error(result, this.startupCallbackId);
}
}
public void onUtteranceCompleted(String utteranceId) {
this.sendJavascript(this.callbackFct);
}
/**
* Clean up the TTS resources
*/
public void onDestroy() {
if (mTts != null) {
mTts.shutdown();
}
}
}
Finally to use it in your javascript code :
window.plugins.tts.setCallbackFct("yourcallbackFunction()");
Regards,
Guillaume Caplain