Start speech recognizer on Android using Phonegap - javascript

Currently I'm making a Phonegap application.
I want to combine augmented reality en speech input.
There is a plugin for Phonegap called SpeechRecognizer, But I can't get it to work.
My header:
<script type="text/javascript" src="cordova-2.6.0.js"></script>
<script type="text/javascript" src="SpeechRecognizer.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function speechOk() {
alert('speech works');
}
function speechFail() {
alert("speech doesn't work");
}
function onDeviceReady() {
window.plugins.speechrecognizer.init(speechOk, speechFail);
}
$("#micButton").bind("touchstart", function() {
var requestCode = 4815162342;
var maxMatches = 1;
var promptString = "What do you want?";
window.plugins.speechrecognizer.startRecognize(speechOk, speechFail, requestCode, maxMatches, promptString);
});
</script>
A picture of the project (config.xml):
Thanks in advance

Is not your fault, the SpeechRecognizer.java has a bug inside.
I had the same problem and I solved it with just replacing the Speech Recognizer plugin with and older version (like 2.0.0), you can download it from github.
It worked for me with Phonegap 2.5.0, guess it works in 2.6.0.

There were a few problems.
First of all, the SDK version wasn't right. If you use the new cordova you also have to use the newest version of the plugin. This version requires SDK 15 or higher. (android manifest -> <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="17" />).
After that, for some reason the plugin init does not return anything.
I just triggerd the: window.plugins.speechrecognizer.startRecognize(); function on a button click, and it executes.
The javascript (you need jQuery for this code):
$("#micButton").bind("touchstart", function() {
var requestCode = 4815162342;
var maxMatches = 1;
var promptString = "What do you want?";
window.plugins.speechrecognizer.startRecognize(speechOk, speechFail, requestCode, maxMatches, promptString);
});
function speechOk(result) {
var match, respObj;
if (result) {
respObj = JSON.parse(result);
if (respObj) {
var response = respObj.speechMatches.speechMatch[0];
$("#searchField").val(response);
$("#searchButton").trigger("touchstart");
}
}
}
function speechFail(m) {
navigator.notification.alert("Sorry, I couldn't recognize you.", function() {}, "Speech Fail");
}
'#micButton' is the button you have to press to start the android voice recognition
'#searchField' is a input field wich gets the result from the voice recognition
Thanks to MrBillau for the good advice.

Related

Dojo function not fired only on IE7

I am new to dojo. I am debugging a js that's using dojo 1.9.1
require(["dojo/ready", "dojo/hash", "dojo/topic"], function (ready, hash, topic) {
ready(function(){
try {
dimXsl = getDimensionXSLT("somepath.xsl"); // 1
topic.subscribe("/dojo/hashchange", callback); // 2
var djConfig = ""; // 3
djConfig.hashPollFrequnecy = 10; // 4
} catch (e) {
console.log('Error ' + e);
}
});
});
function callback() {// blbla }
The above code works for IE8 and above to fire callback(), but not IE7.
My investigations:
I searched Dojo documentation. for version 1.7, the doc https://dojotoolkit.org/reference-guide/1.7/dojo/hash.html suggests a blank html page. So, I did below:
change js dojo link to 1.7 when href it.
Added blank blank.html in project, and replaced //3-4 with
var dojoConfig = "";
dojoConfig.hashPollFrequnecy = 10;
dojoConfig.dojoBlankHtmlUrl = "/blank.html";
Another replacement //3-4 try was:
var dojoConfig = {
hashPollFrequnecy: 10,
dojoBlankHtmlUrl: "/blank.html"
};
But no luck. I also tried to carry above changes with dojo version 1.9.1, but still no luck.
Any ideas please? Thank you very much.

Cordova compass API (navigator.compass.watchHeading) not working (error code 3)

I'm trying to get a sample app running using the cordova compass, but each time the error callback is called with error code 3.
I use cordova V4.0 and of course I added the plugin org.apache.cordova.device-orientation. Here's the code:
<!DOCTYPE html>
<html>
<head>
<title>Compass Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// The watch id references the current `watchHeading`
var gWatchID = null;
// Wait for device API libraries to load
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
function onDeviceReady() {
startWatch();
}
// Start watching the compass
function startWatch() {
// Update compass every 3 seconds
var options = { frequency: 3000 };
if (!gWatchID)
gWatchID = navigator.compass.watchHeading(onSuccess, onError, options);
}
// Stop watching the compass
function stopWatch() {
if (gWatchID) {
navigator.compass.clearWatch(watchID);
gWatchID = null;
}
}
// onSuccess: Get the current heading
function onSuccess(heading) {
var element = document.getElementById('heading');
element.innerHTML = 'Heading: ' + heading.magneticHeading;
}
// onError: Failed to get the heading
function onError(compassError) {
alert('Compass error: ' + compassError.code);
}
</script>
</head>
<body>
<div id="heading">Waiting for heading...</div>
<button onclick="startWatch();">Start Watching</button>
<button onclick="stopWatch();">Stop Watching</button>
</body>
</html>
The app is built, deployed and started successfully. But when it is started, only error code 3 is displayed.
According to the documentation only two error code are definded:
CompassError.COMPASS_INTERNAL_ERR = 0;
CompassError.COMPASS_NOT_SUPPORTED = 20;
So I wonder what's the meaning error code 3?
And what am I doing wrong?
Thanks for your answers,
Dante
Either your device does not have a magnetic sensor, or the vendor has not implemented support for it in the OS.
Looking at the Android source code for the device-orientation plugin, the startup code is written like this (modified for brevity):
List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
// If found, then register as listener
if (list != null)
this.setStatus(CompassListener.STARTING);
// If error, then set status to error
else
this.setStatus(CompassListener.ERROR_FAILED_TO_START);
Not sure why they made up their own error code there (public static int ERROR_FAILED_TO_START = 3), but really they should be reporting COMPASS_NOT_SUPPORTED as defined in the documentation.

how do I make my ipad app pop up windows and making them close

I have a mobile app that is primarily external links.
Works GREAT in Android. Apple rejected it because the popups don't have "close" buttons.
I tried using this code to make that, but the windows open and hide instantly.
Need the script to make the windows open and have some kind of close button.
Help?
NEW<br>new<br> InAppBrowser
<script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
var ref = null;
function openInAppBrowserBlank(url)
{
try {
ref = window.open(encodeURI(url),'_blank','location=yes'); //encode is needed if you want to send a variable with your link if not you can use ref = window.open(url,'_blank','location=no');
ref.addEventListener('loadstop', LoadStop);
ref.addEventListener('exit', Close);
}
catch (err)
{
alert(err);
}
}
function LoadStop(event) {
if(event.url == "http://www.mypage.com/closeInAppBrowser.html"){
// alert("fun load stop runs");
ref.close();
}
}
function Close(event) {
ref.removeEventListener('loadstop', LoadStop);
ref.removeEventListener('exit', Close);
}
</script>

window.location.replace wtih Phonegap

I try setup a JS function to auto load a index page in a different language, regardig the setting device o my reader.
I try with this...but don't work :
<script src="cordova-2.2.0.js"></script>
<script type="text/javascript" charset="utf-8">
function checkLanguage() {
if (navigator.globalization.getPreferredLanguage()='en_EN')
{
window.location.replace("index_en.html");
}
else if (navigator.globalization.getPreferredLanguage()='fr_FR')
{
window.location.replace("index_fr.html");
}
else
{
window.location.replace("index_other.html");
}
}
</script>
Is this method can be use, or do I have to consider other option to deal with my multilanguage app ?
Thanks in advance for any help.
You need to use the callback of getPreferredLanguage:
var handleDeviceReady = function (event)
{
navigator.globalization.getPreferredLanguage(
function (language)
{
console.log("language: " + language.value + '\n');
redirectToLocaleSpecificLogin(language.value);
},
function ()
{
console.log("Error getting language\n");
redirectToLocaleSpecificLogin("en");
}
);
};
document.addEventListener("deviceready", handleDeviceReady, false);
Then inside of your callback (redirectToLocaleSpecificLogin in this case), you can do your redirects.
most of the browsers use language property, IE uses userLanguage
var lang = window.navigator.userLanguage || window.navigator.language;
this should work in IE, SAFARI, CHROME and FF
Edit:
JavaScript for detecting browser language preference this link has more detailed discussion on this topic

PhoneGap/Cordova Plugin - how to set up?

Ive installed the Calendar plugin for iOS successfully (I think) from here:
https://github.com/phonegap/phonegap-plugins/tree/master/iOS/CalendarPlugin
However I just cant seem to get it working.
So far heres what I have done:
Dropped the calendar .h and .m files into the plugins folder of my
project.
Added the calendar.js file to my directory structure and linked in
the header
Added the EventKit and EventKitUI frameworks to my project
Added the term calendarPlugin to my cordova.plist file
And 5. Added the code below to the page one which I want to save:
window.plugins.calendarPlugin.prototype.createEvent = function(title,location,notes, startDate, endDate){
var title= "My Appt";
var location = "Los Felix";
var notes = "me testing";
var startDate = "2012-11-23 09:30:00";
var endDate = "2012-11-23 12:30:00";
cal.createEvent(title,location,notes,startDate,endDate);
}
$(document).ready(function() {
cal = window.plugins.calendarPlugin;
var cal;
$('.calinfo').live('click', function() {
var desiredValue = $(this).parent().prev().find('.calendar').val();
console.log(desiredValue);
var calInfo = desiredValue.split(',');
createEvent(calInfo[0], calInfo[1], calInfo[2], calInfo[3], calInfo[4]);
});
});
When I run it noting happens. Have I missed something?
Dont't use $(document).ready(function());. Rather use device ready. Call the function onBodyLoad() on body load.
function onBodyLoad()
{
document.addEventListener("deviceready", onDeviceReady, false);
deviceready = true;
}
//////////////////////////////////
function onDeviceReady()
{
// Your code goes here
}

Categories