将参数传递给SpeechRecognition Listener onResults

时间:2017-10-19 11:03:28

标签: android json bluetooth speech-to-text

构建一个接收语音输入的应用程序,并通过webhook向IFTTT发出JSON POST请求。

当用户点击蓝牙设备上的按钮时,应用程序:

  1. 启动语音识别
  2. 返回一段转录的语音
  3. 根据按下的按钮构建自定义URL(来自作为参数传递的事件名称)
  4. 向构建的URL发送JSON(包括语音字符串)POST请求
  5. 在SpeechRecognitionListener的onResults中,我调用构建URL的函数并发出JSON POST请求。但是,我无法将用于构建URL的特定事件名称传递给onResults。所需的功能是拥有一个将eventName作为参数的方法,而在onResults中,使用此名称发送webrequest。因此,该方法可以重用于多个不同的事件。例如:

    SpeechIftttWebhook(event_name);
    

    试图找出解决此问题的最佳方法。我现在使用的代码,其中事件名称被硬编码为公共变量:

    @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                                            BluetoothGattCharacteristic characteristic) {
    
         if (action.contains("trigger1")) {
            Handler mainHandler = new Handler(Looper.getMainLooper());
            Runnable myRunnable = new Runnable() {
                @Override
                public void run () {
    
                    mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                            PACKAGE_NAME);
    
                    SpeechRecognitionListener listener = new SpeechRecognitionListener();
                    mSpeechRecognizer.setRecognitionListener(listener);
                    mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
                }
            };
            mainHandler.post(myRunnable);
         }
    }
    

    这是SpeechRecognitionListener类:

    class SpeechRecognitionListener implements RecognitionListener {
        @Override
        public void onBeginningOfSpeech(){
            Log.i("SPEECH", "onBeginningOfSpeech");
        }
    
        @Override
        public void onBufferReceived(byte[] buffer) {
        }
    
        @Override
        public void onEndOfSpeech() {
            Log.i("SPEECH", "onEndOfSpeech");
        }
    
        @Override
        public void onError(int error) {
            Log.i("SPEECH", "onError: " + error);
            if (error == 7) {
                mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
            } else {
                mSpeechRecognizer.destroy();
            }
        }
    
        @Override
        public void onEvent(int eventType, Bundle params) {
        }
    
        @Override
        public void onPartialResults(Bundle partialResults) {
        }
    
        @Override
        public void onReadyForSpeech(Bundle params)
        {
            Log.d(TAG, "onReadyForSpeech");
        }
    
        @Override
        public void onResults(Bundle results) {
            ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            Log.i("SPEECH_RESULT", String.valueOf(results));
    
            String note = matches.get(0);
            Log.i("SPEECH_RESULT_FIRST", speechText);
    
            //Get the event name and key from database
            String url = BuildIftttWebhookUrl(event_name, ifttt_key);
            Log.i("LINK", url);
    
            JSONObject json = buildIftttJson(note, null, null);
    
            makeJsonPostRequest(json, url);
        }
    
        @Override
        public void onRmsChanged(float rmsdB) {
        }
    }
    

    以下是构建IFTTT网址的方法:

    public String BuildIftttWebhookUrl(String event, String key) {
        return ("https://maker.ifttt.com/trigger/" + event + "/with/key/" + key);
    }
    

    以下是构建JSON的方法:

    public JSONObject BuildIftttJson (String value1, String value2, String value3) {
        JSONObject iftttJson = new JSONObject();
        try {
            iftttJson.put("value1", value1);
            iftttJson.put("value2", value2);
            iftttJson.put("value3", value3);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return iftttJson;
    }
    

    以下是JSON POST请求方法:

    public String makeJsonPostRequest(JSONObject iftttJson, String url) {
        final HttpURLConnection urlConnection;
        final String json = iftttJson.toString();
        Log.e("jsonn==> ", json);
        final String[] result = {null};
        try {
            //Connect
            urlConnection = (HttpURLConnection) new URL(url).openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestMethod("POST");
    
            AsyncTask taskConnect = new AsyncTask() {...}
            AsyncTask taskWrite = new AsyncTask() {...}
            AsyncTask taskRead = new AsyncTask() {...}
    

0 个答案:

没有答案