mirror of
https://github.com/purplecabbage/phonegap-plugins.git
synced 2026-04-24 03:00:11 -04:00
Merge pull request #402 from poiuytrez/master
Speech to text - Get Supported languages method
This commit is contained in:
@@ -7,57 +7,94 @@ A collection of possible matches (strings) are returned to your app.
|
||||
|
||||
Of course this plugin requires [Android PhoneGap](http://github.com/phonegap/phonegap-android).
|
||||
|
||||
1. To install the plugin, copy SpeechRecognizer.js to your project's www folder.
|
||||
2. Add SpeechRecognizer.js to your html file, eg: `<script type="text/javascript" charset="utf-8" src="SpeechRecognizer.js"></script>`
|
||||
1. To install the plugin, copy speechrecognizer.js to your project's www folder.
|
||||
2. Add speechrecognizer.js to your html file, eg: `<script type="text/javascript" charset="utf-8" src="speechrecognizer.js"></script>`
|
||||
3. Create an 'com/urbtek/phonegap' path under 'src' and add the SpeechRecognizer.java file to it
|
||||
4. Add the plugin to the 'res/xml/plugins.xml' file. eg: `<plugin name="SpeechRecognizer" value="com.urbtek.phonegap.SpeechRecognizer"/>`
|
||||
|
||||
### Example
|
||||
```javascript
|
||||
function onDeviceReady()
|
||||
{
|
||||
window.plugins.speechrecognizer.init(speechInitOk, speechInitFail);
|
||||
// etc.
|
||||
}
|
||||
```html
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>PhoneGap</title>
|
||||
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
|
||||
<script type="text/javascript" charset="utf-8" src="speechrecognizer.js"></script>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
function onLoad(){
|
||||
document.addEventListener("deviceready", onDeviceReady, true);
|
||||
}
|
||||
function onDeviceReady()
|
||||
{
|
||||
window.plugins.speechrecognizer.init(speechInitOk, speechInitFail);
|
||||
// etc.
|
||||
}
|
||||
|
||||
function speechInitOk() {
|
||||
// we're good
|
||||
}
|
||||
function speechInitFail(m) {
|
||||
// recognizer not present?
|
||||
}
|
||||
function speechInitOk() {
|
||||
alert("we are good");
|
||||
supportedLanguages();
|
||||
recognizeSpeech();
|
||||
}
|
||||
function speechInitFail(m) {
|
||||
alert(m);
|
||||
}
|
||||
|
||||
function recognizeSpeech() {
|
||||
var requestCode = 1234;
|
||||
var maxMatches = 5;
|
||||
var promptString = "Please say a command";
|
||||
window.plugins.speechrecognizer.startRecognize(speechOk, speechFail, requestCode, maxMatches, promptString);
|
||||
}
|
||||
|
||||
function speechOk(result) {
|
||||
var match, respObj, requestCode;
|
||||
if (result) {
|
||||
respObj = JSON.parse(result);
|
||||
if (respObj) {
|
||||
// This is the code that was sent with the original request
|
||||
requestCode = respObj.speechMatches.requestCode;
|
||||
|
||||
for (match in respObj.speechMatches.speechMatch) {
|
||||
console.log("possible match: " + respObj.speechMatches.speechMatch[match]);
|
||||
// regex comes in handy for dealing with these match strings
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function speechFail(m) {
|
||||
console.log("speechFail: " + m.toString());
|
||||
}
|
||||
// Show the list of the supported languages
|
||||
function supportedLanguages() {
|
||||
window.plugins.speechrecognizer.getSupportedLanguages(function(languages){
|
||||
// display the json array
|
||||
alert(languages);
|
||||
}, function(error){
|
||||
alert("Could not retrieve the supported languages");
|
||||
});
|
||||
}
|
||||
|
||||
function recognizeSpeech() {
|
||||
var requestCode = 1234;
|
||||
var maxMatches = 5;
|
||||
var promptString = "Please say a command"; // optional
|
||||
var language = "en-US"; // optional
|
||||
window.plugins.speechrecognizer.startRecognize(speechOk, speechFail, requestCode, maxMatches, promptString, language);
|
||||
}
|
||||
|
||||
function speechOk(result) {
|
||||
var respObj, requestCode, matches;
|
||||
if (result) {
|
||||
respObj = JSON.parse(result);
|
||||
if (respObj) {
|
||||
var matches = respObj.speechMatches.speechMatch;
|
||||
|
||||
for (x in matches) {
|
||||
alert("possible match: " + matches[x]);
|
||||
// regex comes in handy for dealing with these match strings
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function speechFail(message) {
|
||||
console.log("speechFail: " + message);
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body onload="onLoad();">
|
||||
<h1>Welcome to PhoneGap</h1>
|
||||
<h2>Edit assets/www/index.html</h2>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
## RELEASE NOTES ##
|
||||
|
||||
### November 23, 2011 ###
|
||||
|
||||
* Java code to java convention (functions starts with a lowercase characater)
|
||||
* New getSupportedLanguages method
|
||||
* Java file warnings removed
|
||||
* Better error handling
|
||||
* New language parameter
|
||||
* SpeechOk demo code fixed (show the recognized text instead of the id)
|
||||
|
||||
### September 16, 2011 ###
|
||||
|
||||
* Initial release
|
||||
@@ -66,7 +103,10 @@ function speechFail(m) {
|
||||
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2011 Colin Turner (github.com/koolspin)
|
||||
Copyright (c) 2011
|
||||
Colin Turner (github.com/koolspin)
|
||||
Guillaume Charhon (github/poiuytrez)
|
||||
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
* Speech Recognition PhoneGap plugin (Android)
|
||||
*
|
||||
* @author Colin Turner
|
||||
* @author Guillaume Charhon
|
||||
*
|
||||
* Copyright (c) 2011, Colin Turner
|
||||
* Copyright (c) 2011, Colin Turner, Guillaume Charhon
|
||||
*
|
||||
* MIT Licensed
|
||||
*/
|
||||
@@ -23,20 +24,53 @@ import com.phonegap.api.PluginResult.Status;
|
||||
|
||||
import android.util.Log;
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.speech.RecognizerIntent;
|
||||
|
||||
class HintReceiver extends BroadcastReceiver {
|
||||
com.urbtek.phonegap.SpeechRecognizer speechRecognizer;
|
||||
String callBackId = "";
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
if (getResultCode() != Activity.RESULT_OK) {
|
||||
return;
|
||||
}
|
||||
// the list of supported languages.
|
||||
ArrayList<CharSequence> hints = getResultExtras(true).getCharSequenceArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
|
||||
|
||||
// Convert the map to json
|
||||
JSONArray languageArray = new JSONArray(hints);
|
||||
PluginResult result = new PluginResult(PluginResult.Status.OK, languageArray);
|
||||
result.setKeepCallback(false);
|
||||
//speechRecognizer.callbackId = "";
|
||||
speechRecognizer.success(result, "");
|
||||
}
|
||||
|
||||
public void setSpeechRecognizer(SpeechRecognizer speechRecognizer){
|
||||
this.speechRecognizer = speechRecognizer;
|
||||
}
|
||||
|
||||
public void setCallBackId(String id){
|
||||
this.callBackId = id;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Style and such borrowed from the TTS and PhoneListener plugins
|
||||
*/
|
||||
|
||||
public class SpeechRecognizer extends Plugin {
|
||||
private static final String LOG_TAG = SpeechRecognizer.class.getSimpleName();
|
||||
public static final String ACTION_INIT = "init";
|
||||
public static final String ACTION_SPEECH_RECOGNIZE = "startRecognize";
|
||||
public static final String NOT_PRESENT_MESSAGE = "Speech recognition is not present or enabled";
|
||||
|
||||
private String speechRecognizerCallbackId = "";
|
||||
public String callbackId = "";
|
||||
private boolean recognizerPresent = false;
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -47,7 +81,7 @@ public class SpeechRecognizer extends Plugin {
|
||||
// Dispatcher
|
||||
if (ACTION_INIT.equals(action)) {
|
||||
// init
|
||||
if (DoInit())
|
||||
if (doInit())
|
||||
return new PluginResult(Status.OK);
|
||||
else
|
||||
return new PluginResult(Status.ERROR, NOT_PRESENT_MESSAGE);
|
||||
@@ -57,16 +91,27 @@ public class SpeechRecognizer extends Plugin {
|
||||
if (!recognizerPresent) {
|
||||
return new PluginResult(PluginResult.Status.ERROR, NOT_PRESENT_MESSAGE);
|
||||
}
|
||||
if (!(this.speechRecognizerCallbackId.length() == 0)) {
|
||||
|
||||
if (!this.callbackId.isEmpty()) {
|
||||
return new PluginResult(PluginResult.Status.ERROR, "Speech recognition is in progress.");
|
||||
}
|
||||
|
||||
this.speechRecognizerCallbackId = callbackId;
|
||||
this.callbackId = callbackId;
|
||||
startSpeechRecognitionActivity(args);
|
||||
PluginResult res = new PluginResult(Status.NO_RESULT);
|
||||
res.setKeepCallback(true);
|
||||
return res;
|
||||
}
|
||||
else if("getSupportedLanguages".equals(action)){
|
||||
// save the call back id
|
||||
//this.callbackId = callbackId;
|
||||
// Get the list of supported languages
|
||||
getSupportedLanguages();
|
||||
// wait for the intent callback
|
||||
PluginResult res = new PluginResult(Status.NO_RESULT);
|
||||
res.setKeepCallback(true);
|
||||
return res;
|
||||
}
|
||||
else {
|
||||
// Invalid action
|
||||
String res = "Unknown action: " + action;
|
||||
@@ -75,31 +120,47 @@ public class SpeechRecognizer extends Plugin {
|
||||
}
|
||||
|
||||
/**
|
||||
* Request the supported languages
|
||||
*/
|
||||
private void getSupportedLanguages() {
|
||||
// Create and launch get languages intent
|
||||
Intent intent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
|
||||
HintReceiver hintReceiver = new HintReceiver();
|
||||
hintReceiver.setSpeechRecognizer(this);
|
||||
//hintReceiver.setCallBackId(this.callbackId);
|
||||
ctx.getApplicationContext().sendOrderedBroadcast(intent, null, hintReceiver, null, Activity.RESULT_OK, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the speech recognizer by checking if one exists.
|
||||
*/
|
||||
private boolean DoInit() {
|
||||
this.recognizerPresent = IsSpeechRecognizerPresent();
|
||||
private boolean doInit() {
|
||||
this.recognizerPresent = isSpeechRecognizerPresent();
|
||||
return this.recognizerPresent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a recognizer is present on this device
|
||||
*/
|
||||
private boolean IsSpeechRecognizerPresent() {
|
||||
private boolean isSpeechRecognizerPresent() {
|
||||
PackageManager pm = ctx.getPackageManager();
|
||||
List activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
|
||||
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
|
||||
return !activities.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Fire an intent to start the speech recognition activity.
|
||||
*
|
||||
* @param args Argument array with the following string args: [req code][number of matches][prompt string]
|
||||
*/
|
||||
|
||||
private void startSpeechRecognitionActivity(JSONArray args) {
|
||||
int reqCode = 42; //Hitchhiker?
|
||||
int maxMatches = 0;
|
||||
String prompt = "";
|
||||
String language = "";
|
||||
|
||||
try {
|
||||
if (args.length() > 0) {
|
||||
@@ -116,6 +177,10 @@ public class SpeechRecognizer extends Plugin {
|
||||
// Optional text prompt
|
||||
prompt = args.getString(2);
|
||||
}
|
||||
if (args.length() > 3){
|
||||
// Optional language specified
|
||||
language = args.getString(3);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
Log.e(LOG_TAG, String.format("startSpeechRecognitionActivity exception: %s", e.toString()));
|
||||
@@ -123,6 +188,11 @@ public class SpeechRecognizer extends Plugin {
|
||||
|
||||
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
|
||||
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
|
||||
// If specific language
|
||||
if(!language.equals("")){
|
||||
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
|
||||
}
|
||||
|
||||
if (maxMatches > 0)
|
||||
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxMatches);
|
||||
if (!(prompt.length() == 0))
|
||||
@@ -138,17 +208,20 @@ public class SpeechRecognizer extends Plugin {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
// Fill the list view with the strings the recognizer thought it could have heard
|
||||
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
|
||||
ReturnSpeechResults(requestCode, matches);
|
||||
speechResults(requestCode, matches);
|
||||
|
||||
}
|
||||
else {
|
||||
// Failure - Let the caller know
|
||||
ReturnSpeechFailure(resultCode);
|
||||
else if(resultCode == Activity.RESULT_CANCELED){
|
||||
// cancelled by user
|
||||
speechFailure("Cancelled");
|
||||
} else {
|
||||
speechFailure("Unknown error");
|
||||
}
|
||||
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
private void ReturnSpeechResults(int requestCode, ArrayList<String> matches) {
|
||||
private void speechResults(int requestCode, ArrayList<String> matches) {
|
||||
boolean firstValue = true;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{\"speechMatches\": {");
|
||||
@@ -169,14 +242,14 @@ public class SpeechRecognizer extends Plugin {
|
||||
|
||||
PluginResult result = new PluginResult(PluginResult.Status.OK, sb.toString());
|
||||
result.setKeepCallback(false);
|
||||
this.success(result, this.speechRecognizerCallbackId);
|
||||
this.speechRecognizerCallbackId = "";
|
||||
this.success(result, this.callbackId);
|
||||
this.callbackId = "";
|
||||
}
|
||||
|
||||
private void ReturnSpeechFailure(int resultCode) {
|
||||
PluginResult result = new PluginResult(PluginResult.Status.ERROR, Integer.toString(resultCode));
|
||||
private void speechFailure(String message) {
|
||||
PluginResult result = new PluginResult(PluginResult.Status.ERROR, message);
|
||||
result.setKeepCallback(false);
|
||||
this.error(result, this.speechRecognizerCallbackId);
|
||||
this.speechRecognizerCallbackId = "";
|
||||
this.error(result, this.callbackId);
|
||||
this.callbackId = "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* Speech Recognizer PhoneGap plugin (Android)
|
||||
*
|
||||
* @author Colin Turner
|
||||
*
|
||||
* @author Guillaume Charhon
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
@@ -31,9 +31,22 @@ SpeechRecognizer.prototype.init = function(successCallback, errorCallback) {
|
||||
* @param reqCode User-defined integer request code which will be returned when recognition is complete
|
||||
* @param maxMatches The maximum number of matches to return. 0 means the service decides how many to return.
|
||||
* @param promptString An optional string to prompt the user during recognition
|
||||
* @param language is an optional string to pass a language name in IETF BCP 47. If nothing is specified, the currrent phone language is used
|
||||
*/
|
||||
SpeechRecognizer.prototype.startRecognize = function(successCallback, errorCallback, reqCode, maxMatches, promptString) {
|
||||
return PhoneGap.exec(successCallback, errorCallback, "SpeechRecognizer", "startRecognize", [reqCode, maxMatches, promptString]);
|
||||
SpeechRecognizer.prototype.startRecognize = function(successCallback, errorCallback, reqCode, maxMatches, promptString, language) {
|
||||
return PhoneGap.exec(successCallback, errorCallback, "SpeechRecognizer", "startRecognize", [reqCode, maxMatches, promptString, language]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the list of the supported languages in IETF BCP 47 format
|
||||
*
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
*
|
||||
* Returns an array of codes in the success callback
|
||||
*/
|
||||
SpeechRecognizer.prototype.getSupportedLanguages = function(successCallback, errorCallback) {
|
||||
return PhoneGap.exec(successCallback, errorCallback, "SpeechRecognizer", "getSupportedLanguages", []);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
* A plugin to enable iOS In-App Purchases.
|
||||
*
|
||||
* Copyright (c) Matt Kane 2011
|
||||
* Copyright (c) Guillaume Charhon 2012
|
||||
*/
|
||||
|
||||
var InAppPurchaseManager = function() {
|
||||
@@ -46,14 +47,14 @@ InAppPurchaseManager.prototype.restoreCompletedTransactions = function() {
|
||||
InAppPurchaseManager.prototype.requestProductData = function(productId, successCallback, failCallback) {
|
||||
var key = 'f' + this.callbackIdx++;
|
||||
window.plugins.inAppPurchaseManager.callbackMap[key] = {
|
||||
success: function(productId, title, description, price ) {
|
||||
if (productId == '__DONE') {
|
||||
delete window.plugins.inAppPurchaseManager.callbackMap[key]
|
||||
return;
|
||||
}
|
||||
successCallback(productId, title, description, price);
|
||||
},
|
||||
fail: failCallback
|
||||
success: function(productId, title, description, price ) {
|
||||
if (productId == '__DONE') {
|
||||
delete window.plugins.inAppPurchaseManager.callbackMap[key]
|
||||
return;
|
||||
}
|
||||
successCallback(productId, title, description, price);
|
||||
},
|
||||
fail: failCallback
|
||||
}
|
||||
var callback = 'window.plugins.inAppPurchaseManager.callbackMap.' + key;
|
||||
PhoneGap.exec('InAppPurchaseManager.requestProductData', productId, callback + '.success', callback + '.fail');
|
||||
@@ -111,46 +112,37 @@ InAppPurchaseManager.prototype.onRestoreCompletedTransactionsFailed = null;
|
||||
/* This is called from native.*/
|
||||
|
||||
InAppPurchaseManager.prototype.updatedTransactionCallback = function(state, errorCode, errorText, transactionIdentifier, productId, transactionReceipt) {
|
||||
alert(state);
|
||||
switch(state) {
|
||||
case "PaymentTransactionStatePurchased":
|
||||
if(this.onPurchased) {
|
||||
this.onPurchased(transactionIdentifier, productId, transactionReceipt);
|
||||
} else {
|
||||
this.eventQueue.push(arguments);
|
||||
this.watchQueue();
|
||||
}
|
||||
if(window.plugins.inAppPurchaseManager.onPurchased)
|
||||
window.plugins.inAppPurchaseManager.onPurchased(transactionIdentifier, productId, transactionReceipt);
|
||||
|
||||
return;
|
||||
|
||||
case "PaymentTransactionStateFailed":
|
||||
if(this.onFailed) {
|
||||
this.onFailed(errorCode, errorText);
|
||||
} else {
|
||||
this.eventQueue.push(arguments);
|
||||
this.watchQueue();
|
||||
}
|
||||
if(window.plugins.inAppPurchaseManager.onFailed)
|
||||
window.plugins.inAppPurchaseManager.onFailed(errorCode, errorText);
|
||||
|
||||
return;
|
||||
|
||||
|
||||
case "PaymentTransactionStateRestored":
|
||||
if(this.onRestored) {
|
||||
this.onRestored(transactionIdentifier, productId, transactionReceipt);
|
||||
} else {
|
||||
this.eventQueue.push(arguments);
|
||||
this.watchQueue();
|
||||
}
|
||||
if(window.plugins.inAppPurchaseManager.onRestored)
|
||||
window.plugins.inAppPurchaseManager.onRestored(transactionIdentifier, productId, transactionReceipt);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
InAppPurchaseManager.prototype.restoreCompletedTransactionsFinished = function() {
|
||||
if (this.onRestoreCompletedTransactionsFinished) {
|
||||
this.onRestoreCompletedTransactionsFinished();
|
||||
}
|
||||
if (this.onRestoreCompletedTransactionsFinished) {
|
||||
this.onRestoreCompletedTransactionsFinished();
|
||||
}
|
||||
};
|
||||
|
||||
InAppPurchaseManager.prototype.restoreCompletedTransactionsFailed = function(errorCode) {
|
||||
if (this.onRestoreCompletedTransactionsFailed) {
|
||||
this.onRestoreCompletedTransactionsFailed(errorCode);
|
||||
}
|
||||
if (this.onRestoreCompletedTransactionsFailed) {
|
||||
this.onRestoreCompletedTransactionsFailed(errorCode);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -197,8 +189,8 @@ InAppPurchaseManager.prototype.eventQueue = [];
|
||||
InAppPurchaseManager.prototype.timer = null;
|
||||
|
||||
PhoneGap.addConstructor(function() {
|
||||
if(!window.plugins) {
|
||||
window.plugins = {};
|
||||
}
|
||||
window.plugins.inAppPurchaseManager = InAppPurchaseManager.manager = new InAppPurchaseManager();
|
||||
});
|
||||
if(!window.plugins) {
|
||||
window.plugins = {};
|
||||
}
|
||||
window.plugins.inAppPurchaseManager = InAppPurchaseManager.manager = new InAppPurchaseManager();
|
||||
});
|
||||
Reference in New Issue
Block a user