简体   繁体   中英

request google speech to text API using android volley

I'm working on an Android Application that record a speech using the mic of the android device, after that i encode the recorded speech into base64 format. to transform the speech into a text i use google speech cloud api and android Volley to send request to google Api. the probleme is when i call the methode for requesting i got this error ==>

E/Volley: [5921] BasicNetwork.performRequest: Unexpected response code 400 for https://speech.googleapis.com/v1/speech:recognize?key= {mykey}

Here is My Code :

    public class SpeechService {

    private String Url ="https://speech.googleapis.com/v1/speech:recognize?key=${mykey}";

        private RequestQueue requestQueue ;
        private AppCompatActivity app ;
        String savedata ;

        public SpeechService(AppCompatActivity app){

            this.app = app ;
            requestQueue = Volley.newRequestQueue(app.getApplicationContext());
        }

    public void getTextFromGSpeech1(String EncodedSpeech){

        JSONObject jsonObject = new JSONObject();
        JSONObject config = new JSONObject() ;
        JSONObject audio = new JSONObject() ;

        try {

            config.put("encoding", MediaRecorder.OutputFormat.AMR_WB);
            config.put("SampleRateHertz","rba9 rba9");
            config.put("languageCode","fr-FR");
            audio.put("content",EncodedSpeech);

            jsonObject.put("audio", audio );
            jsonObject.put("config", config);

        } catch (JSONException e) {
            e.printStackTrace();
        }


        JsonObjectRequest JObjectREQUEST = new JsonObjectRequest(Request.Method.POST , Url  , jsonObject ,new Response.Listener<JSONObject>(){

            @Override
            public void onResponse(JSONObject response) {

                System.out.println("JSON response is  ============> ");
                System.out.println(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                System.out.println("failed ...............;");

            }
        }){

            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }

        };

        requestQueue.add(JObjectREQUEST);

    }
}

and the Json object format must be like this

 {   "audio": {
            "content": ""         
          }
            ,
          "config": {
            "encoding": "ENCODING_UNSPECIFIED",
            "languageCode": "fr-FR",
            "sampleRateHertz": 1600
          }
        }

It should be RecognitionConfig.AudioEncoding.AMR_WB , not MediaRecorder.OutputFormat.AMR_WB . Sample rate is also wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM