我正在开发一个Android应用程序,该应用程序使用Android设备的麦克风录制语音,之后我将录制的语音编码为base64格式。为了将语音转换为文本,我使用谷歌语音云api和安卓Volley向谷歌api发送请求。问题是当我调用请求的方法时,我得到了这个错误===>
E/Volley:[5921]BasicNetwork.performRequest:的意外响应代码400https://speech.googleapis.com/v1/speech:recognize?key={mykey}
这是我的代码:
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);
}
}
Json对象格式必须像这个
{ "audio": {
"content": ""
}
,
"config": {
"encoding": "ENCODING_UNSPECIFIED",
"languageCode": "fr-FR",
"sampleRateHertz": 1600
}
}
它应该是RecognitionConfig.AudioEncoding.AMR_WB
,而不是MediaRecorder.OutputFormat.AMR_WB
。采样率也是错误的。