2013-04-16 26 views
1

目前我正在製作Phonegap應用程序。 我想結合增強現實和語音輸入。 Phonegap有一個名爲SpeechRecognizer的插件,但我無法使其工作。使用Phonegap在Android上啓動語音識別器

我的頭:

<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> 

項目(config.xml文件)的圖片: enter image description here

在此先感謝

+0

通過關注github回購上的readme.md,我得到了它的正常工作。你確定一切拼寫正確嗎?最初我遇到了麻煩,因爲我把它打錯爲** speechRecgonizer **。另外...你的設備上是否有語音識別器應用程序?它適用於Nexus7,但在沒有語音識別器應用程序的較舊設備上運行失敗。 「'onDeviceReady()''中的console.log(window.plugins.speechrecognizer)''會發生什麼?最後,你的日誌說什麼? – MBillau

+0

這很奇怪。我無法讓它工作。我的控制檯日誌:「未捕獲TypeError:無法讀取屬性'undefined'的語音識別器' 你有什麼想法:你的設備上是否有語音識別器應用程序?我有一個索尼Xperia Z(android 4.1.2),所以不能成爲問題的權利? – Tober

+0

我已經在Android清單中將min sdk版本更改爲15,現在我不再有任何控制檯錯誤,但現在它不記錄任何內容。 – Tober

回答

1

有幾個問題。 首先,SDK版本不對。如果你使用新的科爾多瓦,你還必須使用最新版本的插件。該版本需要SDK 15或更高版本。 (android manifest - ><uses-sdk android:minSdkVersion="15" android:targetSdkVersion="17" />)。 之後,出於某種原因,插件init不返回任何內容。 我只是觸發了:window.plugins.speechrecognizer.startRecognize();點擊按鈕上的功能,然後執行。

的JavaScript(你需要的jQuery此代碼):

$("#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' 是你必須按下啓動Android的語音識別

按鈕 '#searchField' 是一個輸入領域至多從語音識別得到結果

感謝MrBillau的好建議。

+3

我已更新插件以刪除無用的init方法。該插件在那裏可用:https://github.com/poiuytrez/SpeechRecognizer/。如果您遇到任何錯誤,請創建問題單。 – poiuytrez

2

是不是你的錯,SpeechRecognizer.java裏面有一個bug。

我有同樣的問題,我只是用Speech Recognizer插件替換舊版本(如2.0.0),解決了它,你可以從github下載它。

它爲我工作Phonegap 2.5.0,猜測它在2.6.0的作品。